Search code examples
androidandroid-listviewhorizontalscrollview

Highlight selected image in HorizantalListView in android and it will remain selected untill another image is not clicked


I am working with HorizantalListView but when i tapped on any element of list then i want to show it selected and it should remain selected until another image is not clicked how its possible please help me.

enter image description here


Solution

  • Make it a toggle button instead of an imageView. And then create a selector on the xml with the various images you wish on each corresponind state. (I'm not sure now if the same can be done with imageView that's why I told you to use toggle button).

    <?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/button_pressed"/>
        <item android:state_pressed="false"
                android:drawable="@drawable/button_rested"/>
        <item android:state_enabled="false"
                android:drawable="@drawable/button_disabled"/>
    </selector>
    

    on the laybout

    <ToggleButton android:id="@+id/image" android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:background="@null" android:gravity="center_horizontal" 
         android:src="@drawable/button_selector"/>