ListView: press state selector

In this tutorial we will add visual indication to ListView pressed state.

























Step 1: In res/drawable folder create a new file "selector_item_gradient.xml"



<?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/gradient_pressed"/>
    <item android:state_focused="true"
            android:drawable="@drawable/gradient_focused"/>
    <item android:state_selected="true"
            android:drawable="@drawable/gradient_selected"/>
    <item android:drawable="@drawable/gradient_normal"/>
</selector>




Step 2: In res/drawable create file "gradient_normal.xml" that is gray.


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient
            android:startColor="#fffdfdfe"
            android:centerColor="#ffe7e7e8"
            android:endColor="#ffb1afa9"
            android:angle="270"/>
</shape>

Step 3: in res/drawable create file "gradient_pressed.xml" that is orange or yellow.


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient
            android:startColor="#fffdfdfe"
            android:centerColor="#ffe7e7e8"
            android:endColor="#ffb1afa9"
            android:angle="270"/>
</shape> 

Step 4: In the root element of your ITEM list_item_book.xml add following



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:drawSelectorOnTop="false"
              android:background="@drawable/selector_item_gradient"






As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles