Android ListView with EditText loses focus when the keyboard displays

The issue is when you have a ListView or an ExpandableListView with input capable fields that displays the soft keyboard on focus, the EditText loses its focus for the first time but the second time it just works fine. The reason it happens is all the views are getting rendered again, so the EditText field object in the item row that used to be focused is now a completely different object.

You can solve this by adding android:descendantFocusability="beforeDescendants" in your ListView and android:windowSoftInputMode="adjustPan" for your activity in the app Manifest. Here are some examples

<ExpandableListView
 android:id="@+id/productList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/search"
    android:layout_below="@+id/search"
    android:padding="5dp" 
    android:descendantFocusability="beforeDescendants"/>
        
<activity android:name=".myActivity"
 android:label="@string/app_name"
 android:screenOrientation="sensorPortrait"
 android:windowSoftInputMode="adjustPan"/>

If the above solution doesn't work then you have to listen for the onFocusChange listener on the EditText and set a variable to keep track. So when the view get rendered again in your getView or getChildView method you can set the focus back to that EditText.

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.