3

I am aware that I can set the background color of a ListView dynamically through some variation of the following code:

ListView mainListView;  
mainListView = (ListView) findViewById( R.id.listView1 );
mainListView.setBackgroundColor(Color.BLACK);

However, I would like to do the identical thing through XML instead. I tried the following code without any luck (it makes no changes):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        **android:background="#FFFFFF"**
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>
</RelativeLayout>

Does anyone know of a simple way to change the background color of a ListView via XML?

John Roberts
  • 5,885
  • 21
  • 70
  • 124
  • I think you are inflating a costum row layout for your `listView` ? Then may b setting background of your `listitem`, will be helpful. – Mohsin Naeem Jul 19 '12 at 03:53

3 Answers3

5

You can do it this way:

You have to create the Color.xml file in the res/value folder of your project. The code of Color.xml is

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="orange">#ff5500</color>
 <color name="white">#ffffff</color>
 <color name="transparent">#00000000</color>
 <color name="date_color">#999999</color>
 <color name="black">#000000</color>
 <color name="gray">#999999</color>
 <color name="blue">#0066cc</color>
 <color name="gold">#e6b121</color>
 <color name="blueback">#99FFFF</color>
 <color name="articlecolor">#3399FF</color> 
 <color name="article_title">#3399FF</color> 
 <color name="cachecolor">#8ad0e8</color>
</resources>

Web colors in an Android color xml resource file

Or, try this:

android:background="@android:color/holo_green_light"

with these colors:

http://developer.android.com/reference/android/R.color.html

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
1

"#ffffff" should work. But you could also use : android:background="@android:color/black"

Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

Since a list item has several states (pressed, focussed, selected, etc), its best to use a selector xml than a single color. See dev doc and this stack overflow question for reference. Once you create the selector, set this as the android:background.

Community
  • 1
  • 1
Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39