Skip to content Skip to sidebar Skip to footer

Change Listview Item Background Color But Keep The Default Selector Style?

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 setting the backgroun

Solution 1:

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.

Solution 2:

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) {
            ViewrowView= 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;
        }

Solution 3:

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"

Solution 4:

Try to apply this style to your ListView:

<stylename="List"><itemname="android:layout_width">fill_parent</item><itemname="android:layout_height">fill_parent</item><itemname="android:background">#FFFFFF</item><itemname="android:dividerHeight">1dp</item><itemname="android:divider">#CCCCCC</item><itemname="android:cacheColorHint">#FFFFFF</item><itemname="android:listSelector">#00000000</item></style>

And in your xml layout this:

<ListView 
    android:id="@+id/listView"
    style="@style/List"
    android:paddingTop="1dp"/>

Post a Comment for "Change Listview Item Background Color But Keep The Default Selector Style?"