35

I have a ListView where I've defined the layout of each item in a separate XML file. In this file I've included a RatingBar and an EditText.

I've programmatically created 7-8 items in this ListView. When I scroll through them, it seems to be quite buggy. Here are some examples:

  1. If I set focus to the EditText in the first row and then scroll down the ListView, random EditTexts from other rows will have focus. It seems to be that the next EditText after the focused one disappears receives focus. Perhaps this is intentional, but, as a user, it seems very weird.

  2. If I set focus to an EditText, receive a virtual keyboard, type something, and click the "Done" button on my virtual keyboard, the EditText will empty as soon as the virtual keyboard disappears.

  3. Sometimes, when I click an EditText, receive a virtual keyboard and start typing letters, the letters will disappear as soon as I type them.

  4. When I click on an EditText, the virtual keyboard shows itself, but the EditText loses focus and I have to click the EditText again.

  5. Even though I've set the RatingBar to focusable="false", if I move my scrollwheel, it still grabs focus.

One of my problems is all the visible list items get redrawn when I type a character in the virtual keyboard (and since the text of the EditText is set to some data, which is empty, it gets cleared. I don't understand why Android would decide to redraw the list every time I type a character.

Here is the XML I'm using to draw them. They are white bubbles, with a gray boarder, and some text, a RatingBar and an EditText inside:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:background="@drawable/shape_outer">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="2dip"
            android:background="@drawable/shape_inner">
                    <TextView
                            android:id="@+id/rating_category"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/dark_gray"
                            android:textStyle="bold"
                            android:layout_marginBottom="10dip" />
                        <RatingBar 
                            android:id="@+id/rating_rating"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:numStars="5"
                            android:rating="0"
                            android:stepSize="1"
                            android:focusable="false"
                            android:clickable="false"
                            />
                        <EditText 
                            android:id="@+id/rating_text"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_column="1"
                            android:padding="6dip"
                            android:textColor="#000000"
                            android:gravity="left|top"
                            android:lines="3"
                            android:hint="Comment"
                            android:imeOptions="actionDone" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • 8
    I just up voted because i love your question name. How powerful is your virtual device? – Nix Aug 12 '10 at 14:35
  • Lol thank you. The only thing I did when creating the virtual device (I just typed "VD" here and figured I'd spell it out instead) was type a name and select the target (2.1). Everything else is default. – Andrew Aug 12 '10 at 14:38
  • What seems to be happening here is all the visible list items get redrawn when I type a character in the virtual keyboard (and since the text of the EditText is set to some data, which is empty, it gets cleared. Why is typing text redrawing the list item???? – Andrew Aug 12 '10 at 14:45
  • I have a buggy ListView with form elements in it as well. User responses are disappearing when they're scrolled out of view. After finding this post I'm sad that I have to recode it, but happy to know I shouldn't look for an answer anymore. +1 – Nick Hagianis May 25 '11 at 14:47
  • 1
    Same here. I'm more frustrated than sad though, and wish I had known its limitations earlier. – Jeshurun Jun 28 '12 at 23:27
  • Item 4 is a dupe of [Focusable EditText inside ListView](http://stackoverflow.com/q/2679948) – blahdiblah Nov 01 '13 at 01:01

5 Answers5

19

It sounds like ListViews aren't able to handle EditTexts well. I've done some research and the consensus seems to be "don't do that." So what I've resorted to is creating a simple layout file which is a ScrollView with a LinearLayout inside. In my onCreate method, I inflate the View I was using for my list item and add it to the LinearLayout. I'm also adding the View to an ArrayList so I can save the data in each View later on.

Does this sound reasonable?

Andrew
  • 20,756
  • 32
  • 99
  • 177
  • This post was great. I lost a lot of time trying to get text and dropdown inputs working inside a ListView. After I read this, I reimplemented using a LinearLayout within a ScrollView, and it worked perfectly the first time. – Sky Kelsey Oct 10 '11 at 21:18
  • This happens when EditText's are not in ListView's as well (my EditText's are nested within a few LinearLayout's and RelativeLayout's). – samus Jan 15 '13 at 22:27
  • @SamusArin is there a known solution to this issue when you have EditTexts nested within several LinearLayouts? – stephen Sep 07 '13 at 10:09
  • @stephen see if this cant help (2nd answer down): http://stackoverflow.com/questions/15161261/viewpager-focus-issue ... btw, were not supposed to chat here, but you can post a question if my answer doesn't suffice (email me the questions link at samusarin@gmail.com if you do post). – samus Sep 09 '13 at 20:55
  • Does somebody have a implemented solution for that? i'm also having problems with listview with edittext, and I don't know how to change to scrollview. Thanks! – Marco Altran Sep 04 '14 at 21:45
5

Well,add this to your activity in manifest...

android:windowSoftInputMode="adjustPan"

Example...

<activity android:name=".mapview.AddParkingLotActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"/>
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Sreekanth Karumanaghat
  • 3,383
  • 6
  • 44
  • 72
  • Adding android:windowSoftInputMode="adjustPan" to my activity in the manifest solved this issue. Another tip that would be appreciated is a way to add padding so that the edittext field is not smashed against the soft keyboard panel. – Rob Aug 07 '14 at 07:01
  • this works for me, but could be a good idea explain us what's that ; ) – Ging3r Sep 03 '15 at 15:53
4

I was having the same problem. My EditText inside of LisView was losing focus when the keyboard appears, so on getView from the ListView item I put

final EditText editInput = (EditText)convertView.findViewById(R.id.edit_input);
editInput.requestFocusFromTouch();

And this work for me.

  • 2
    EDIT: It doesnt work. It seemed to work fine with samsung keyboard but the issue persists on SwiftKey keyboard. – VsMaX Dec 31 '13 at 01:45
3

These two steps resolved a similar issue I had (EditText on a row within ListView + funny keyboard behavior).

  1. Add android:windowSoftInputMode="adjustPan" attribute into AndroidManifest.xml file on the activity where the ListView is presented.
  2. Add android:descendantFocusability="afterDescendants" attribute onto the ListView itself.

This is the actual example:

AndroidManifest.xml

    <activity
        android:name=".SubscriptionListActivity"
        android:windowSoftInputMode="adjustPan"
        android:label="@string/title_activity_subscriptions" >
    </activity>

layout.xml file

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/subscriptionList" />

I tested it on Samsung Galaxy S3 and it works fine with Samsung as well as SwiftKey keyboards.

pepan
  • 678
  • 6
  • 11
0

i have some issue i solved just commenting this line

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
ashish
  • 217
  • 1
  • 11