Sun
Sun

Reputation: 6888

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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/list_normal" />

    <item
        android:state_selected="true"
        android:state_focused="false"
        android:drawable="@drawable/list_hover" 
        />

    <item 
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/list_hover" />
</selector>

list_normal.xml:

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

list_hover.xml:

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

and finally using list_selector in ListView but seems not working for me don't know why its happening ?

  <ListView 
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:listSelector="@drawable/list_selector"/>    

list_adapter.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:textStyle="bold"
        android:maxLines="2"
        android:text="@string/str_list_name"
        android:textColor="@android:color/background_dark"
        android:layout_marginLeft="2dp"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:padding="10dp"
        android:background="@null"
        android:contentDescription="@null"
        android:src="@drawable/available" />

</RelativeLayout>

activity_listview.xml:

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

    <include
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/header_main" />

    <RelativeLayout 
        android:id="@+id/bottoms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <LinearLayout 
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#c9c9ce"
        android:paddingTop="10dp"
        android:orientation="horizontal">

    ............

    </LinearLayout>


    <LinearLayout 
        android:id="@+id/center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/top" >

    .........

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_height="wrap_content">


       ............

    </LinearLayout>

    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"
            android:layout_marginBottom="5dp"/>

    <ListView 
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:listSelector="@drawable/list_selector"/>    

    </LinearLayout>


    <LinearLayout 
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >

    .............

    </LinearLayout>

    </RelativeLayout>

</LinearLayout>

Upvotes: 0

Views: 1077

Answers (3)

droid
droid

Reputation: 184

Instead of android:state_selected use android:state_enabled in list_selector.xml

Use the correct state according to your requirement..

android:state_pressed

Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.

android:state_focused

Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.

android:state_selected

Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.

android:state_enabled

Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

Use the android:state_activated="true" state, instead of the "selected" state. That will work Example

Upvotes: -1

Dhwanik Gandhi
Dhwanik Gandhi

Reputation: 685

i think the problem is in your list_selector.xml try replace following xml code

  <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item
   android:state_enabled="true" 
   android:state_pressed="true"
   android:drawable="@drawable/list_hover" />
   <item
   android:state_enabled="true"
   android:state_focused="true"
   android:drawable="@drawable/list_hover" />
  <item
   android:state_enabled="true"
   android:state_selected="true"
   android:drawable="@drawable/list_hover" />
  <item
   android:drawable="@drawable/list_normal" />
 </selector>

Upvotes: 3

Related Questions