I have one list view. I have assigned alternate row colors like below in adapter's getView() method :
if(position % 2 ==1)
{
convertView.setBackgroundColor(Color.rgb(231, 249, 255));
}
else
{
convertView.setBackgroundColor(Color.rgb(195, 240, 255));
}
Above code is working fine. Now I want to change the color of the selected list item. For that I have followed the below procedure:
I have created on listSelector xml like below:
<?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>
and I have assigned the above selector to my list as below:
myList.setSelector(R.drawable.list_selector);
But I am not getting selected list item color.
Can anyone please tell me how to set alternate row color and selected list item color to an android list.
Try like this
artists_list_backgroundcolor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/normal" />
<item android:state_pressed="true"
android:drawable="@color/itemselected" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/itemselected" />
</selector>
colors.xml
<resources>
<color name="normal">#ffffff</color>
<color name="itemselected">#EDEDED</color>
</resources>
And use it in getView()
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.HERE_FOR_ALTERNATE);
}
For reference Link
Hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With