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

9

65

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.

Proud answered 5/12, 2013 at 17:18 Comment(5)
Hi, I guess what happens here is not that the EditText should be pressed one more time, in order to be operational, but the fact that the softkeyboard appears, because it is triggered by a field, which is editable and gains focus at first. You can try this public void hideSoftKeyboard(View v) { Activity activity = (Activity) v.getContext(); InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); }.Carnatic
It will prevent the softkeyboard to appear at first and I guess that the EditText would have to just be pressed in order to trigger the editing in it.Carnatic
@g00dy, the problem is not that the keyboard appears initially but entering text does not work properlyProud
ok, then are you sure that the problematic EditText gains focus the first time the keyboard appears? What I mean is - where is the RequestFocus situated?Carnatic
A Link to this could be found here-> developer.android.com/guide/topics/resources/…Carnatic
P
44

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?

Petronilapetronilla answered 12/12, 2013 at 4:29 Comment(3)
Thanks for your answer, unfortunately we can't change the layout in our app, so we have to use listViews for this porpuse. And we already use the dialogs atm for entering text :) A lot of the answers to the questions are accepted though, I wonder if it worked for thise peopleProud
@GrafOrlov you probably don't want to use this solution in production. Consider showing an edittext in a dialog when a row is clicked.Petronilapetronilla
@KyleIvey can you help answer this question please? #37358515Reichel
N
40

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"
Nitrogen answered 16/6, 2014 at 21:41 Comment(1)
This will cause the keyboard to block the EditText if it's at the bottom of the listStrappado
O
19

Modify your manifest xml to add windowSoftInputMode in your activity:

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustPan">
</activity>
Orel answered 16/2, 2015 at 8:50 Comment(0)
G
8

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.

Gereron answered 16/3, 2015 at 16:19 Comment(3)
This is the best option IMHO. The other solutions are only workarounds. They don't change the fact that ListView creates/destroys views en masse during notifyDataSetChanged. With RecyclerView, you notify about individual inserts, updates, and deletes. The focused view is not being recreated so no issues with focus.Machinegun
@PēterisCaune Listview doesn't destroy views en masse ever, it recycles. And 99% of the recycler view code I've ever seen just does the same thing. Recycler view can be more focused, but in practice its just easier not to do it that way.Sigfrid
Happens in a recycler view as wellLandau
L
4

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"

Landau answered 3/7, 2017 at 23:15 Comment(0)
R
2

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.

Ruck answered 10/11, 2015 at 1:3 Comment(1)
Welcome to SO! Please realize this question dates from 2013 (almost 2 years ago). You can still answer of course, especially when techniques and available functions evolve and open room for better solutions. In that case, please provide more detailed explanation on why it would be better than the previous solutions. Finally, please kindly reproduce the essential of the solution here, as a link may become invalid and prevent future users from accessing the resource.Mavismavra
P
1

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.

Pontifical answered 18/1, 2016 at 16:55 Comment(0)
W
1

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

Waterford answered 1/2, 2017 at 6:36 Comment(1)
I agree. I tried EVERYTHING to fix the behaviour of EditText in ListView, and NOTHING worked. I migrated to using RecyclerView (see developer.android.com/guide/topics/ui/layout/recyclerview) and it all works like a dream, with no nasty hacks. And the migration was easy because I was already using the ViewHolder model in my ListView adapter.Somerset
S
0

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;
                }
            }
        });
Sarcoid answered 18/5, 2016 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.