user1395885
user1395885

Reputation: 56

how to change selected list item background, android

I have two ListView's in my Activity, when i select one from first List data will reflected to other list. I have done this part, its working fine for me. But I am struggling to change the background for selected item in first List, how can i do this?

Upvotes: 2

Views: 4557

Answers (2)

Chirag.T
Chirag.T

Reputation: 756

Just set in your main layout of list item view

style="?attr/SelectableItemBackground"

Upvotes: 0

Praveenkumar
Praveenkumar

Reputation: 24476

You can do this by below -

Set the android:listSelector="@drawable/selection" to your ListView's xml attribute. And, in your selection.xml contains the selector which will hold the change when you select any item from listview.

selection.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

Have a look at this also.

Upvotes: 4

Related Questions