Android ListView listSelector not working
NickName:KousiK Ask DateTime:2014-03-15T17:54:56

Android ListView listSelector not working

I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted.

Please help.

Thanks in advance.

ListView.xml

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/listViewBell"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
                android:choiceMode="singleChoice"
                android:divider="#b5b5b5"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/list_selector_color" >

            </ListView>

        </LinearLayout>

list_selectror_color.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <solid android:color="@color/list_selector" />

        <stroke
            android:dashWidth="2dp"
            android:width="1dp"
            android:color="@color/white" />

    </shape>

I have also tried with selector but nothing happens

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/list_selector_color" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_selector_color" android:state_focused="true"/>
    <item android:drawable="@drawable/list_selector_color" android:state_selected="true"/>
    <item android:drawable="@drawable/list_selector_normal"/>

</selector>

Here is my Custom adapter getView Method

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.listview_bell_items, parent,
                false);

        ImageView imageview = (ImageView) convertView
                .findViewById(R.id.list_bell_image);
        imageview.setImageResource(mDataImage[position]);

        TextView textview = (TextView) convertView
                .findViewById(R.id.txt_bell_title);
        textview.setText(mDataText[position]);

        return convertView;
    }

Copyright Notice:Content Author:「KousiK」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22422196/android-listview-listselector-not-working

Answers
Drew 2014-03-17T10:38:03

I had similar problem. Sorry, cannot comment, so I post a possible answer. \n\n\nRemove android:listSelector=\"@drawable/list_selector_color\" property from your ListView declaration\nIn your R.layout.listview_bell_items specify your custom selector for a root layout. E.g., if root layout of your list item is RelativeLayout, try:\n<RelativeLayout ... android:background=\"@drawable/listitem_selector\">...\n\n\nThe same goes for any other type of Layout.\n\nIf this still still does not give you the result you want, provide more details.\n\nUpdate\nOk, if nothing else helps, there's a temporary dirty workaround of your problem. I do not see why it would not work.\n\nIntroduce a selectedPos variable holding currently selected item.\n\nprivate class MyAdapter extends .../*your base adapter*/ { \n private static final int NOT_SELECTED = -1;\n private int selectedPos = NOT_SELECTED;\n\n // if called with the same position multiple lines it works as toggle\n public void setSelection(int position) {\n if (selectedPos == position) {\n selectedPos = NOT_SELECTED;\n } else {\n selectedPos = position;\n }\n notifyDataSetChanged();\n }\n\n @Override\n public View getView(int position, View convertView, ViewGroup parent) {\n View view = super.getView(position, convertView, parent);\n if (position == selectedPos) {\n // your color for selected item\n view.setBackgroundColor(Color.parseColor(\"#000000\"));\n } else {\n // your color for non-selected item\n view.setBackgroundColor(Color.parseColor(\"#FFFFFF\"));\n }\n return view;\n }\n}\n\n\nNow, add the following code after you create and set ListView's adapter:\n\nfinal MyAdapter adapter = new MyAdapter(...);\nlistView.setAdapter(adapter);\nlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {\n @Override\n public void onItemClick(AdapterView<?> parent, View view, int position, long id) {\n adapter.setSelection(position);\n }\n});\n",


More about “Android ListView listSelector not working” related questions

ListSelector on Listview not working

I have my custom listselector like this : &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item

Show Detail

Hanged listSelector in ListView

I am implementing ListView with android:listSelector &lt;style name="ListView" parent="@android:style/Widget.ListView"&gt; &lt;item name="android:cacheColorHint"&gt;@color/transparent&lt;/

Show Detail

Android ListView listSelector not working

I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted. Please ...

Show Detail

ListView listSelector not hiding

I have a ListView with a listSelector drawable to indicate the selected item. &lt;ListView android:id="@+id/roadSignListView" android:layout_width="match_parent" android:layout_height="

Show Detail

ListView listSelector

I am creating an android app which is having a listview. Everything is working fine but when i click on any element of list-view ORANGE shade appears.. in order to remove that i wrote &lt;Li...

Show Detail

Android ListView displays ListSelector only after scrolling

I added a simple ListSelector to the ListView. The Problem is, that the ListSelector is only displayed, after the ListView is scrolled a little. How can I make the ListSelector to apear immediatel...

Show Detail

ListView listSelector seems not working

Using listSelector property to change the list item color whenever user do tap on that, for that i am using list_selector.xml: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:andr...

Show Detail

How does listSelector of ListView work?

Romain Guy himself states that TouchMode does not have any selection or focus in this link. I used android:listSelector="?android:colorPressedHighlight" for a listview and the items I touched retai...

Show Detail

Listview listselector not working

I am creating some views in my xml file, xml file contaings list-view. In that list-view I have list selector attribute and i defined drawable image for selector. My xml file follows like this: ...

Show Detail

Unable to apply a custom listSelector to ListView

I have gone over several SO answers now Android listselector not visible in custom listview ListView item background via custom selector and even followed this to the point (currently using this

Show Detail