When I tap on an EditText (see the "Your answer here ..." field is the screen-cap below) which is inside a ListFragment it does not remain in focus and this makes it impossible to use. As soon as the text cursor appears on the EditText field it quickly disappears and the EditText field looses focus.
I have found few related stack posts and suspect something might be stealing the focus from the EditText. See related post here. I have also read posts on forcing focus but nothing makes sense.
I could build the view programmatically however it is much easier to use the standard ListFragment and ArrayAdapter code. The list will grow with user input and it's easier to manage with the infrastructure that Android provides.
See my screen below. On the right hand side of the screen is a ListFragment which contains custom layouts for the rows. Each row contains an EditText and a Button to submit the text.
Here is the code for the ListFragment:
public class ItemDetailFragment extends ListFragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The dummy content this fragment is presenting.
*/
private static DetailArrayAdapter mAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<DetailData> items = new ArrayList<DetailData>();
items.add(new DetailData("1"));
mAdapter = new DetailArrayAdapter(getActivity(), R.layout.detail_layout, items);
// TODO: replace with a real list adapter.
setListAdapter(mAdapter);
}
class DetailArrayAdapter extends ArrayAdapter<DetailData> {
Context context = null;
int layoutResourceId;
ArrayList<DetailData> data;
public DetailArrayAdapter(Context context, int layoutResourceId, ArrayList<DetailData> data){
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
DetailData data = this.data.get(position);
EditText text = (EditText) row.findViewById(R.id.answer);
text.setText(data.detail_number + "You answer here ...");
Button submit_button = (Button) row.findViewById(R.id.answer_submit);
submit_button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int count = mAdapter.getCount();
int item_num = count + 1;
DetailData d = new DetailData(""+item_num);
mAdapter.add(d);
}
});
return row;
}
}
class DetailData {
public String detail_number;
public DetailData(String num){
this.detail_number = num;
}
}
}
Here is the layout code for the custom row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Your answer here..."
android:maxLines="1"
android:singleLine="true"/>
<Button
android:id="@+id/answer_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</LinearLayout>