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

AndroidAndroid ListviewAndroid EdittextAndroid SoftkeyboardOntouchlistener

Android Problem Overview


I know there are a lot of similar questions out here but I couldn't get any of the provided solutions working in a simple sample app.

The problem occurs when the softkeyboard is shown for the first time. As soon as it is shown, only by pressing the editText again makes it editable.

Tried the following:

 android:windowSoftInputMode="adjustPan|adjustResize"

This is not solving any issues. It seems that this line is mandatory to have the activity resized after the softkeyboard is popping up. Unfortunately, it's also causing any EditTexts to lose focus. This is probably to the ListView itself gaining focus after the resizing process. So I tried the following workaround:

 listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

This always causes the first visible EditText that the ListView contains to gain focus, which is undesirable. The second EditText in the second row should instead gain focus when pressed, which is not happening. Also, if I eventually managed to focus another EditText other then the first one shown (e.g. by pressing 'Next' on the softkeyboard), the first visible one will receive focus after the keyboard is dismissed and the ListView being resized to its full size again.

I tried several other things like intercepting onFocusChange() events for the ListView, while knowing which EditText was pressed by its TouchListener. Requesting the focus for that certain EditText again did not lead to any success either.

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

Android Solutions


Solution 1 - Android

A classic hack for situations like this is to use a handler and postDelayed(). In your 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 keep this code out of production. I also wouldn't be surprised if the focus bug manifests itself differently in different android versions or roms.

There are also many other problems with embedding an EditText within a ListView that have solutions that feel like a hack. See all of the other people struggling.

It's also very easy to have something like this happen:

like this.

After having gone down similar paths many times myself, I've mostly given up on trying to override any of the default keyboard behaviours or quirks. I would recommend trying to find alternative solution in your app if possible.

Have you considered having the ListView rows be just a styled TextView and then displaying a Dialog with an EditText when a row is clicked, updating the TextView as necessary?

Solution 2 - Android

I was having problems with the ActionBar "stealing" focus when I pressed on an EditText located within a 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

Basically I added this to my ListView:

android:descendantFocusability="beforeDescendants"

and added this to my activity:

android:windowSoftInputMode="adjustPan"

Solution 3 - Android

Modify your manifest xml to add windowSoftInputMode in your activity:

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustPan">
</activity>

Solution 4 - Android

I know its a very old thread but this answer might be helpful to someone so here it is:

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

Solution 5 - Android

I was having the same problem with recyclerView and trying all suggested solutions.

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

Solution 6 - Android

Use Recycler View, this solves several issues from list and gridview. You can even work with staggered gridviews. You could easily start working with this.

Solution 7 - Android

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

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

Whenever the Listview doesn't cover the softkeyboard, everything is fine.

Solution 8 - Android

was having the same issue. Searched for all such solutions with inputMode, focusability et al. Best solution, migrate to recycler view.

Solution 9 - Android

In my case I've added local value currentlyFocusedRow in my Adapter. In the method getView() I've add these 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;
                }
            }
        });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDisplay nameView Question on Stackoverflow
Solution 1 - AndroidKyle IveyView Answer on Stackoverflow
Solution 2 - AndroidgonzobrainsView Answer on Stackoverflow
Solution 3 - Androidnlmm01View Answer on Stackoverflow
Solution 4 - AndroidShahrozKhan91View Answer on Stackoverflow
Solution 5 - AndroidOmerView Answer on Stackoverflow
Solution 6 - Androiduser1176776View Answer on Stackoverflow
Solution 7 - AndroidKai WangView Answer on Stackoverflow
Solution 8 - AndroidAman AgarwalView Answer on Stackoverflow
Solution 9 - AndroidKiryl BielašeŭskiView Answer on Stackoverflow