Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change color of android list item on click or selection? [duplicate]

Tags:

android

Possible Duplicate:
How to change color of ListView items on focus and on click

friends,

i want to change color on android list item click any one guide me which attribute should i use to achieve this functionality?

actually when user clicks on list item he does not know if it was clicked or not?

here is my code.

<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:drawSelectorOnTop="false"
        android:layout_below="@+id/Tablayoutdesign"
        android:cacheColorHint="#000000"
        android:dividerHeight="1dip"
        android:layout_marginTop="63dip"
        android:layout_marginBottom="40dip"
        />
like image 611
UMAR-MOBITSOLUTIONS Avatar asked Dec 06 '10 11:12

UMAR-MOBITSOLUTIONS


Video Answer


1 Answers

Step1: Embbed the android:listSelector attribute in your ListView as Shown Below.

<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/Tablayoutdesign"
        android:cacheColorHint="#000000"
        android:dividerHeight="1dip"
        android:layout_marginTop="63dip"
        android:layout_marginBottom="40dip"

        />

Step2: Create a new xml named listselector and put the following code in that

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 

</selector> 

Step3: Create a file named colors.xml add the following the code in that file

<resources>
    <drawable name="focused">#ff5500</drawable>
    <drawable name="selected">#FF00FF</drawable>
</resources>

Add this line in your java code

ListView lv= (ListView) findViewById(R.id.list);
lv.setSelector( R.drawable.listselector);
like image 184
Sankar Ganesh PMP Avatar answered Oct 22 '22 00:10

Sankar Ganesh PMP