After unsuccessfully trying to create an interface with EditTexts
inside a ListView
all I can say to you is "Don't!". You'll be in much more trouble when you have enough items that you have to scroll your ListView
, focus will jump here and there, you'll have to save your EditText
's state and so on. It seems like general idea is that using EditText
in ListView
is not worth it.
After quite a bit of research I can suggest the following method that helped me:
I've inherited ListView
and overrided layoutChildren method, inside it I do the following:
@Override
protected void layoutChildren() {
super.layoutChildren();
final EditText tv = (EditText)this.getTag();
if (tv != null)
{
Log.d("RUN", "posting delayed");
this.post(new Runnable() {
@Override
public void run() {
Log.d("RUN", "requesting focus in runnable");
tv.requestFocusFromTouch();
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
}
});
}
}
When I know which EditText
should get the focus (I know this when my adapters getView is called) I set this particular EditText
as a tag to ListView
. Then ListView lays out itself and my post thread is queued. It runs and requests focus, however since it wasn't enough in my case, I also generate two MotionEvents
that simply simulate a tap. Apparently this is enough to make the soft keyboard appear.
The reasons behind this are explained in an answer here:
Android Actionbar Tabs and Keyboard Focus