EditText in Listview loses focus when clicked on Android 4.x

I know that there are many similar questions here, but I could not get any of the provided solutions working in a simple example application.

The problem occurs when the first page of the softkeyboard is displayed. Once this is shown, just clicking editText again makes it editable.

Tried the following:

android:windowSoftInputMode="adjustPan|adjustResize" 

This is not a solution to any problems. This line seems to be required to resize the activity after the tooltip appears. Unfortunately, this also causes any EditTexts to lose focus. This is probably for the ListView that got the focus after the resize process. So I tried the following workaround:

  listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 

This always invokes the first visible EditText that contains the ListView to receive focus, which is undesirable. The second EditText in the second line should instead receive focus when pressed, which does not happen. Also, if I eventually manage to focus another EditText on another, and then the first one is shown (for example, by clicking "Next" on the softkeyboard ), the first visible one will get focus after the keyboard is rejected and the ListView is resized to full size again.

I tried several other things, for example, to intercept the onFocusChange() events for a ListView, knowing which EditText was pressed by its TouchListener . Setting the focus for this some EditText again did not lead to any success.

Using a ScrollView instead of a ListView , as suggested by other users, is not an option for the respective project.

+59
android android-softkeyboard android-listview android-edittext ontouchlistener
Dec 05 '13 at 17:18
source share
9 answers

A classic hack for such situations is to use a handler and postDelayed() . In the adapter:

 private int lastFocussedPosition = -1; private Handler handler = new Handler(); public View getView(final int position, View convertView, ViewGroup parent) { // ... edittext.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { handler.postDelayed(new Runnable() { @Override public void run() { if (lastFocussedPosition == -1 || lastFocussedPosition == position) { lastFocussedPosition = position; edittext.requestFocus(); } } }, 200); } else { lastFocussedPosition = -1; } } }); return convertView; } 

This works on my device, but this code does not work. I also won’t be surprised if the focus error manifests itself differently in different versions of Android or ROMs.

There are also many other problems with embedding EditText inside a ListView , which has solutions that look like hacks. See All Other People Who Fight.

It is also very easy to do something like this:

like this .

After going many similar paths many times, I basically gave up trying to override any default keyboard behavior or quirks. I would recommend trying to find an alternative solution in your application, if possible.

Do you think ListView strings are just stylized TextView and then display Dialog with EditText when you click a row, updating the TextView if necessary?

+39
Dec 12 '13 at 4:29
source share

I was having trouble stealing the ActionBar when I clicked on the EditText located in the ListView row. The above solutions did not work, but the following solution worked for me:

http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html

I basically added this to my ListView:

 android:descendantFocusability="beforeDescendants" 

and added this to my activity:

 android:windowSoftInputMode="adjustPan" 
+36
Jun 16 '14 at 21:41
source share

Modify your xml manifest to add windowSoftInputMode to your activity:

 <activity android:name=".YourActivity" android:windowSoftInputMode="adjustPan"> </activity> 
+18
Feb 16 '15 at 8:50
source share

I know its very old thread, but this answer may be useful for someone, here it is:

Switch to RecyclerView and you don't have to worry about these annoying ListView issues. Instead of making a new appearance, it recycles and reuses old views.

+7
Mar 16 '15 at 16:19
source share

Use recycler View, it solves several problems from the list and gridview. Yo can even play chess in a checkerboard pattern. Yo can easily get started with this http://android-er.blogspot.com.co/2015/07/staggeredgridlayoutmanager-google-app.html

+1
Nov 10 '15 at 1:03
source share

When the list is long enough to cover the soft keyboard, the EditText in the Listview loses focus when you click on Android 4.x.

One solution is to wrap a Listview in a linear layout with a height of half the screen.

Whenever Listview does not close the soft keyboard, everything is fine.

+1
Jan 18 '16 at 16:55
source share

In my case, I added the local value currentFocusedRow in my adapter. In the getView () method, I add this code to each editText:

  if (currentlyFocusedRow == position) { editText.requestFocus(); } editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { if (currentlyFocusedRow == -1) { currentlyFocusedRow = position; } } else { currentlyFocusedRow = -1; } } }); 
0
May 18 '16 at 15:08
source share

had the same problem. Searched for all such solutions with inputMode, focusability et al. The best solution, switch to viewing recyclers.

0
Feb 01 '17 at 6:36
source share

I had the same problem with recyclerView and trying all proposed solutions.

Finally, the problem in my case was that recyclerView had wrap_content as a value for height on my XML by accident; changed it to match_parent and started working as expected, the focusable value focusable set and used by android:windowSoftInputMode="adjustResize"

0
Jul 03 '17 at 23:15
source share



All Articles