Android list item does not display selected color

I have this list:

<ListView
            android:id="@+id/contactsView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/searchButton"
            android:divider="@color/DarkGoldenrod"
            android:dividerHeight="0.1dp"
            android:listSelector="@drawable/list_selector"
            android:choiceMode="singleChoice" />

This is the code for list_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:drawable/myRedColor" android:state_pressed="true"/>
    <item android:drawable="@android:drawable/myBlueColor" android:state_selected="true"/>
    <item android:drawable="@android:drawable/myGreenColor"/>

</selector>

In my main activity class, I set the following:

contactsView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                arg1.setSelected(true);
                selectedContactIndex = (int) arg3;
            }
        });

contactsView- this is a list, and I confirmed that it is selected, but the color does not remain. I click on it, and it changes, and then comes back. Any idea why?

+4
source share
3 answers

Instead android:listSelector="@drawable/list_selector"in ListView use it as background in row.xml

+5
source

You can do this also on a layout in which your list has a layout background

android:background="?android:attr/activatedBackgroundIndicator"

And then in your work

ListView.setSelector(R.drawable.list_selector);
+1
source

, , : ( )

<item android:drawable="@android:drawable/myBlueColor" android:state_active="true"/>
<item android:drawable="@android:drawable/myBlueColor" android:state_selected="true"/>
0

All Articles