Ogen
Ogen

Reputation: 6709

Android listview item not showing selected color

I have this list view:

<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 the 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 have put 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 is the list view, and I have confirmed that it IS being selected but the color is not staying. I click it and it changes then changes back. Any idea why?

Upvotes: 5

Views: 3744

Answers (3)

DSS
DSS

Reputation: 7259

Hey how about adding these lines also to the selector you have made: (if it helps)

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

Upvotes: 0

Developer
Developer

Reputation: 6350

You can do on this way also on the layout in which u have your listview set the background of the layout

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

And then in your activity

ListView.setSelector(R.drawable.list_selector);

Upvotes: 1

Richa
Richa

Reputation: 3193

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

Upvotes: 6

Related Questions