Change listview background color in android using XML

How to create listview with custom background color from activity_main.xml layout file using android:background=””.

In this tutorial we are customizing the whole list view background color because by-default list view background color is white and some times app developer needs to setup new color as list view background. So by using android:background=”” attribute on listview widget app developer can easily add background color on listview. So here is the complete step by step tutorial for Change listview background color in android using XML.

android-project-download-code-button

How to Change listview background color in android using XML.

Code for MainActivity.java file.

 package com.changelistviewbackgroundcolor_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listView;
 String[] listValue = new String[] {"Android Examples","Android Studio","SDK Manager","Eclipse"
 ,"Java","JDK"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listView = (ListView)findViewById(R.id.listView1);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);
 
 listView.setAdapter(adapter); 
 
 }
}

Code for activity_main.xml layout file.

 <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"
 tools:context="com.changelistviewbackgroundcolor_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:background="#FFCDD2"
 >
 </ListView>

</RelativeLayout>

Screenshot:

Change listview background color in android using XML

Click here to download Change listview background color in android using XML project with source code.