7

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 background color using the "SetBacgroundResource" method of my item view.

I am using Android 4.0 and I tried the example from this blog http://daniel-codes.blogspot.co.nz/2010/07/how-to-change-listview-rows-background.html but using this example the selector shows however the unselected background color is not showing.

How can I achieve this in Android 4.0 ICS?

EDIT: Here is the Resource I am using for the List Item background drawable.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_window_focused="false" android:state_selected="true" android:drawable="@android:color/transparent" /> 
   <item android:state_selected="true" android:drawable="@android:color/transparent" /> 
   <item android:state_pressed="true" android:state_selected="false" android:drawable="@android:color/transparent" /> 
   <item android:state_selected="false" android:drawable="@color/list_item_first" /> 
</selector>

The code that I am using to set this background drawable is inside the GetView method of my adatapter. The code is:

convertView.setBackgroundResrouce(R.drawable.list_item_dark);
startupsmith
  • 5,554
  • 10
  • 50
  • 71

4 Answers4

4

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.

yDelouis
  • 2,094
  • 17
  • 18
  • Hi Yes I have already tried this... It does work however it isn't exactly the effect that I am after. I don't want the highlight to be displayed over all of the text and images in the list item. – startupsmith Nov 13 '12 at 01:11
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"
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
ingyesid
  • 2,864
  • 2
  • 23
  • 21
  • The problem with this is that I am rotating the background drawables in code so the background drawable will not be the same for all of the rows therefore setting it at the listview level will not work... – startupsmith Nov 08 '12 at 02:06
  • Works perfectly for me. I have a custom ListView with item's background as white and on press I wanted to change it to blue color. – Rohan Kandwal Sep 29 '14 at 07:06
3

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;
        }
KEYSAN
  • 885
  • 8
  • 23
  • these code can't success when i am applying some layout inflate in convertView .. so please suggest me to how to success these condition....!!! – Dhruv Raval Jan 20 '15 at 06:47
1

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"/>
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21