Tuesday, June 7, 2011

Changing the color of android list item on click or selection events?

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);

1 comment:

  1. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he's tryiong none the less. I've been using Movable-type
    on several websites for about a year and am concerned about switching to another platform.
    I have heard excellent things about blogengine.net. Is there a way I can transfer all my wordpress content into it?
    Any help would be greatly appreciated!

    Also visit my web page - pest control brisbane northside

    ReplyDelete

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...