being new to android app development, I encounter a problem regarding the interaction of android fragments, edittexts, focus and buttons.
I have one fragment activity with several tabs. Most tabs have one fragment each. But in one tab, I have a ListFragment
and a fragment with edittexts and buttons, which I call edit fragment. When the user selects a list item, the content of the edit fragment is shown in the current tab. When trying to edit a text there, more precisely, when hitting a different key than the backspace key, the new char is not shown, but the first button below gets the focus. Why? And how can I avoid this behavior?
Earlier, I had a different activity for the edit fragment and the editing worked perfectly. But for other reasons and because it is recommended, I have only one activity now. Thank you for help!
The xml for project_details1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/projectDetails">
<LinearLayout
android:id="@+id/projectDetailsButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#656565"
android:padding="5dp">
<Button android:id="@+id/projectDetailsButton_save"
android:text="@string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2" />
<Button android:id="@+id/projectDetailsButton_cancel"
android:text="@string/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_above="@id/projectDetailsButtons">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dip"
android:background="@color/myColor"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/projectNo" />
<TextView
android:id="@+id/projectDetailsProjectNr"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:text="@string/projectNo"
android:padding="3dip" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/description" />
<EditText
android:id="@+id/projectDetailsProjectDescription"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:inputType="text"
android:padding="3dip" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
/>
<EditText
android:id="@+id/projectDetailsClientName"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:inputType="text"
android:background="@android:drawable/editbox_background"
android:padding="3dip" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/customerNo" />
<EditText
android:id="@+id/projectDetailsClientNr"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:inputType="numberSigned"
android:background="@android:drawable/editbox_background"
android:padding="3dip" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/bookingNo" />
<EditText
android:id="@+id/projectDetailsBookingNr"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:inputType="numberSigned"
android:padding="3dip" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/orderNo" />
<EditText
android:id="@+id/projectDetailsOrderNr"
android:layout_width="0dip"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:inputType="numberSigned"
android:padding="3dip" >
</EditText>
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
the method fillEditTexts
:
private void fillEditTexts(final View view){
final Project project = this.getShownProject();
final EditText projectDescription = (EditText)view.findViewById(R.id.projectDetailsProjectDescription);
final TextView projectNumber = (TextView)view.findViewById(R.id.projectDetailsProjectNr);
final EditText bookingNumber = (EditText)view.findViewById(R.id.projectDetailsBookingNr);
final EditText orderNumber = (EditText)view.findViewById(R.id.projectDetailsOrderNr);
if(project!=null){
projectDescription.setText(project.getProjectDescription() == null ? " ": project.getProjectDescription());
if(project.getProjectNumber()!=null){
projectNumber.setText(project.getProjectNumber().toString());
}
if(project.getBookingNumber()!=null){
bookingNumber.setText(project.getBookingNumber().toString());
}
if(project.getOrderNumber()!=null){
orderNumber.setText(project.getOrderNumber().toString());
}
}
final Button cancel = (Button) view.findViewById(R.id.projectDetailsButton_cancel);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProjectDetailsFragment.this.goBack();
}
});
final Button save = (Button) view.findViewById(R.id.projectDetailsButton_save);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProjectDetailsFragment.this.saveChanges(view);
ProjectDetailsFragment.this.goBack();
}
});
}
Fragment
lifecycle methods correctly? more specifically... did you inflate your layout correctly inonCreateView()
? - Alex Lockwood@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } View view=inflater.inflate(R.layout.project_details1, container, false); this.fillEditTexts(view); return view; }
- JuliafillEditTexts()
method as well as the XML code inres/layout/project_details1
? maybe post it as an edit in your question rather than in the comments so others can help too :) - Alex Lockwood