I am trying to programmatically set a custom background color in my ListView Adapter but I want to keep the default Listview Selector style from Android.
I am settin
Did you try to set the property android:drawSelectorOnTop of your ListView
to true ?
You will be able to set a background for your item and to see the item highlighted when pressed.
Try write your custom adapter and set rowView BackgroundColor like that(if position odd number BLACK, even RED). also you can send a color list to adapter and set different color for every rowView
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
enter code here
if(rowView == null)
{
if((position % 2)==1){
rowView.setBackgroundColor(Color.BLACK)
//colorList :setBackgroundColor( colorList.get(position) )
}
else{
rowView.setBackgroundColor(Color.RED)
}
}
return rowView;
}
Try to apply this style to your ListView:
<style name="List">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:background">#FFFFFF</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:divider">#CCCCCC</item>
<item name="android:cacheColorHint">#FFFFFF</item>
<item name="android:listSelector">#00000000</item>
</style>
And in your xml layout this:
<ListView
android:id="@+id/listView"
style="@style/List"
android:paddingTop="1dp"/>
Try this one :
in your Row layout set the background properties for
android:background="@drawable/your_selector"
and your listView
set
android:listSelector="@drawable/your_selector"