Wednesday 2 April 2014

Quick trick of the week: EditText inside a ListView



Let's suppose you want to create a layout with a ListView containing an EditText in each item, like in the following picture:


If yout set up your layout like the one showed above you'll soon have to deal with an annoying problem: for some unknown reason the EditText immediately loses focus, and it's almost impossible to write anything.
For some devices a good workaround would be to double tap on the EditText, but this is not an accetable solution for the end users of our apps.

Fortunately the solution is quite easy: you just have to add the following lines of code:
  1. in the AndroidManifest.xml file add the following line for the Activity containing the ListView: android:windowSoftInputMode="adjustPan";
  2. in the layout file of the Activity add the following line to your ListView: android:descendantFocusability="beforeDescendants".


Now the EditText(s) should behave as expected...

 <activity android:name="com.androidthetechnicalblog.MyActivity"
   android:configChanges="keyboardHidden|orientation|screenSize"     
   android:windowSoftInputMode="stateHidden|adjustPan"
   android:label="@string/app_name"></activity>         

<ListView
   android:id="@android:id/list"             
   android:descendantFocusability="beforeDescendants"       
   android:layout_width="match_parent"
   android:layout_height="wrap_content"            
   android:layout_marginTop="10dp" />

13 comments:

  1. I'm getting a random crash using this approach. I also see that if you scroll the listview and select some element (for example item number 15 that´s the third visible row) when you scroll up the third row (item number 3) is on focus.

    ReplyDelete
  2. How can you retain the data in the EditText's upon orientation change? In my experience it cannot be done.

    ReplyDelete
    Replies
    1. In my app I used a workaround (not for this specific issue but for another problem) which consists of avoiding that the app is recreated following orientation changes.

      Delete
    2. with this: he sayed: android:configChanges="keyboardHidden|orientation|screenSize"

      Delete
  3. hi Pablo how to clear that problem

    ReplyDelete
  4. Thanks!! It's very useful to me.

    ReplyDelete
  5. Thanks!! It's very useful to me.

    ReplyDelete
  6. I'd heard about windowSoftInputMode but I'd applied it to the layout rather than the manifest.
    Thankyou for putting it so clearly - I'd spent several hours trying to work out why my EditTexts weren't holding their values when closing the softkeyboard.

    ReplyDelete
  7. how to use this options for Dialog? if listview in activity then it is ok. But i have list view in custom dialog... in custom dialog we didn't put in Manifest file. In this case How to use these options???
    thank u

    ReplyDelete
  8. hello

    i am new to android and a little confuse learning the xml layout, can you please include your code for adding edit text inside listview ?

    ReplyDelete
  9. Thanks:) can you explain how can I get the data in the editText fields, o r where can I find a tutorial

    ReplyDelete