ZeeDroid
ZeeDroid

Reputation: 145

ListSelector on Listview not working

I have my custom listselector like this :

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

and my listview :

<?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"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:listSelector="@drawable/listselector">
    </ListView>

my problem is, when i do not click on listview, listview item do not use focused state background (@drawable/bg_list) ...

any one can help me please???

Upvotes: 3

Views: 7383

Answers (4)

erdomester
erdomester

Reputation: 11829

For those still need it: The key is to set the listSelector as background for the list item!

<?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="fill_parent"
    android:background="#819FF7"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mainList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#819FF7"
        />

 </LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lineItem"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector">

     <ImageView android:id="@+id/imageView"
        android:layout_width="96dp"
        android:layout_height="64dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip" 
        android:layout_centerVertical="true"
        android:textSize="18sp"
        android:textColor="#ff000000" 
        android:layout_width="wrap_content"
        />
    <Button 
        android:id="@+id/button" 
        android:gravity="center"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp" 
        android:layout_alignParentRight="true"
        android:focusable="false" 
        android:text="Delete" />

</RelativeLayout>

Upvotes: 5

ZeeDroid
ZeeDroid

Reputation: 145

I have solved my problem, by read some reference.. my listselector should be like this :

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

my listview layout :

<?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"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        **android:listSelector="@android:color/transparent"**>
    </ListView>

</LinearLayout>

and my also adding my list selector into my list item background :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="horizontal" 
    android:padding="15dip"
    android:gravity="center_vertical"
    **android:background="@drawable/listselector"**>

    <ImageView
        android:id="@+id/imageViewlogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dip" />

    <TextView
        android:id="@+id/textViewcategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/textcolor" 
        android:textSize="15sp"/>

</LinearLayout>

thankyou for Jay & danwilkie for answer my question.. :))

Upvotes: 3

danwilkie
danwilkie

Reputation: 854

It looks like your selector xml at the moment is saying to use bg_list_hover when you click on an item, and bg_list when you focus on an item (using a trackball, for example).

If you want to use a custom drawable in the default case when the item is neither clicked nor focused, you need to add an entry for that:

I would use something like this:

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

Upvotes: 0

Jay
Jay

Reputation: 1492

Have you tried calling "requestFocus()" on the listView in your code after setting android:focusable="true" in the xml? From the View docs:

"Call this to try to give focus to a specific view or to one of its descendants. A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode. See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. This is equivalent to calling requestFocus(int, Rect) with arguments FOCUS_DOWN and null."

Upvotes: 0

Related Questions