Thursday, January 10, 2013

ListView : Changing Selection Color

ListViews on the golemarea unit extensively used. we tend to see them in numerous shapes and sizes, withtotally different types of thingsvaried colours and fonts. I wouldn’t say these customizations area unittroublesome to code, however they are doing involve lots of bits and items. As you'll have complete, the technique is fairly distinctive and not therefore obvious. I’ll attempt to break it up into bits and items that area unit short and hopefully sweet.

This trick involves exploitation one thing called android:listSelector. primarily a listing selector is employed to specify the looks of the chosen list item (I’ve appear articles suggesting that they will even be wont tomodification the background color of individual items) which can be in one among the subsequent states:
  • android:state_focused
  • android:state_pressed
  • none of the above  
  • Lets start by first defining the colors we’re going to use for these states.
    I am going to start by defining the appearance of the selected item that has focus. This can be a simple color or a reference to a Drawable resource (e.g., PNG ). I am using a gradient shape below : listview_selector_focussed.xml:
    01<?xml version="1.0" encoding="utf-8"?>
    02
    03<shape xmlns:android="http://schemas.android.com/apk/res/android"
    04android:shape="rectangle">
    05
    06<gradient
    07android:startColor="#008000"
    08android:endColor="#00FF00"
    09android:angle="90" />
    10
    11</shape>
    The red gradient shape (for selected item that is pressed):listview_selector_pressed.xml:
    01<?xml version="1.0" encoding="utf-8"?>
    02
    03<shape
    05android:shape="rectangle">
    06
    07<gradient
    08android:startColor="#800000"
    09android:endColor="#FF0000"
    10android:angle="90" />
    11
    12</shape>
    The next step is to define the listSelector using something known as item lists or color lists: listview_selector.xml:
    01<?xml version="1.0" encoding="utf-8"?>
    02
    03<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    04
    05<item
    06android:state_focused="true"
    07android:drawable="@drawable/listview_selector_focused" />
    08
    09<item
    10android:state_pressed="true"
    11android:drawable="@drawable/listview_selector_pressed" />
    12
    13</selector>
    This file binds the gradients we’ve defined in the first two code blocks with the focused and pressed state. Note that we don’t define anything for the last state i.e. none of the above. If we do not define an item for a given state the defaults are used.
    The final stage is to bind the selector we defined above to the instance of the list view we’re customizing. In the xml file for the layout that contains the listview, add the following attribute:
    1<ListView
    2...
    3<!-- other attributes omitted -->
    4android:listSelector="@color/listview_selector"
    5/>
    We get the following results:
    Selected item in focus
    When selected item has focus
    Selected Item pressed
    When selected item is pressed

No comments:

Post a Comment