Reputation: 5966
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?
Upvotes: 3
Views: 7987
Reputation: 5278
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.
Upvotes: 0
Reputation: 38163
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
Upvotes: 5
Reputation: 72533
"#ffffff" should work. But you could also use : android:background="@android:color/black"
Upvotes: 1