I have a ListView
with edit text in each items and I want to know how can I make the EditText
editable.. It seems to work fine if I set the android:windowSoftInputMode="adjustPan"
into the manifest but only on some devices. On some devices the soft keyboard covers the edittext.
On the other side, if I don't put android:windowSoftInputMode="adjustPan"
in the manifest, the EditText
looses focus after the soft keyboard is displayed and I have to touch the EditText
multiple times to make it selected and can write in it.
Please help me solve this.
Note: I've seen this post: Focusable EditText inside ListView and nothing helped from there and I have totally other problem than that
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With