SRam
SRam

Reputation: 2830

change backgroundcolor and image of listview when click on listview

hi dear i have 5 rows in my listview each listview have image and text associated with it ,i want when i click on listview at any position it changes its background color and when i click another position in list previosly selected list row comes to its former condition and selected change the color and image ...how to achive it ?thanks

my code is ..but it does not retain previously selected row...

   public void onListItemClick(ListView parent, View v, int position,
                                                                long id) { 
       if(position == 0)
       {
              v.setBackgroundColor(Color.WHITE);                
       }
       if(position == 1)
       {
              v.setBackgroundColor(Color.WHITE);                
       }

   Toast.makeText(getApplicationContext(), "You have selected "
                    +(position+1)+"th item",  Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Views: 1659

Answers (1)

Sunit Kumar Gupta
Sunit Kumar Gupta

Reputation: 1213

You can use the concept of selector. Create a selector xml and use it as the background of the row. it may help you.like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/selector_s" />
    <item android:state_pressed="true" android:drawable="@drawable/selector_s" />
    <item android:drawable="@drawable/selector_d" />  
</selector>

you can use this link

http://android-journey.blogspot.com/2009/12/android-selectors.html

Upvotes: 1

Related Questions