Edit Text in ListActivity ListView loses focus when keyboard comes up

21,652

Solution 1

All you really need to do is apply this to your ListView:

XML:

android:descendantFocusability="afterDescendants"

Java:

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

Solution 2

In my case it perfect worked with adding this like of code to manifest:

android:windowSoftInputMode="adjustPan"

Solution 3

For me a combination of the following helped me (especially number 3):

1) Add the following to the related Activity in the manifest file, this is required for your ListView to be resized to fill only the area above the softKeyboard:

android:windowSoftInputMode="adjustResize"

2) With the below the ListView will ONLY get focus only if none of its descendants want it. The descendants (the EditText) needs to get and keep focus.

<ListView
...
android:descendantFocusability="afterDescendants"/>

3) The automatic showing/hiding of the Spelling Suggestions bar right above the keyboard keeps triggering the ListView to refresh. I turned the Suggestions off for any EditText in my ListView.

<EditText
...
android:inputType="textNoSuggestions|textVisiblePassword" />

Solution 4

recycler view works best and no need to mess around with any other properties anywhere.

Share:
21,652

Related videos on Youtube

Physibeth
Author by

Physibeth

Updated on May 27, 2020

Comments

  • Physibeth
    Physibeth almost 4 years

    I have searched and searched for an answer to this question and everything that looked like an answer has not worked so I guess I will just ask.

    I have a couple of EditText boxes added to a ListView that is the basis to a ListActivity. When I set the windowSoftInputMode to adjustPan and click in an EditText it works until you click on it again and then the keyboard covers it up. When I use adjustResize it works except when the keyboard comes up the EditText loses focus and I have to tap on it again to type.

    I was trying to figure out how to catch the onResize but that seems to be associated with the View and not the activity and I'm not entirely sure how to listen for it. I have also tried all sorts of focusable settings on the EditText boxes and the ListView itself (as suggested in other posts I read) that don't seem to help either.

    • Wolfcow
      Wolfcow almost 13 years
      Is it possible to show a screenshot of what it looks like? Also some code might be helpful.
    • blahdiblah
      blahdiblah over 10 years
      Possible duplicate of Focusable EditText inside ListView
  • Physibeth
    Physibeth almost 13 years
    I was looking into trying to catch the onResize but as it is associated with the view not the activity I'm not entirely sure how to do it. I tried a lot of different variations of setting the focus that I got from other similar questions but none of them seem to help.
  • BarFoo
    BarFoo almost 13 years
    perhaps it would be better to not resize the keyboard and instead move your edittext somewhere that will be above the keyboard? Is there a specific reason you need the textbox where it is?
  • Physibeth
    Physibeth almost 13 years
    Yes, that is the specified design.
  • Physibeth
    Physibeth over 10 years
    Oh my goodness thank you! This came back around as a bigger problem today and so I just saw this answer and it seems to work!
  • Physibeth
    Physibeth over 10 years
    So this solution mostly works. Problem is that the first focusable edit box always gets selected when the SIP comes up. This is a problem if the edit box I'm trying to select isn't the first focusable one. Still trying to work out how to catch this which puts me in a similar place to where I was. Is there a way to set a view's focus order to be first even if it isn't first in the layout?
  • Dany Joumaa
    Dany Joumaa over 10 years
    I have no idea why all the other SO answers recommend beforeDescendants. afterDescendants is the only one that's worked for me, and also makes perfect sense in terms of the docs.
  • Abhishek Mathur
    Abhishek Mathur over 6 years
    thanks a lot , makes me wonder ,why android doesnt do this automatically or provide hints in studio for the same Also this problem is quite common but is not recognised in attention
  • Bojan Kseneman
    Bojan Kseneman over 6 years
    @AbhishekMathur studio is not awere that this view is used in a ListView / RecyclerView
  • drmrbrewer
    drmrbrewer almost 3 years
    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.