Android ListView Text Color

AndroidListview

Android Problem Overview


I am trying to set the ListView textColor to black, since I am using a white background.

Here is my MailActivity

public class MailActivity extends ListActivity {

	String[] listItems = { "Compose", "Inbox", "Drafts", "Sent" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        setContentView(R.layout.mails);

		setListAdapter(new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, listItems));

	}
}

and my XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
    android:layout_width="fill_parent"
	android:layout_height="fill_parent" 
    android:background="#FFFFFF">
	
    <ListView 
        android:id="@android:id/list" 
        android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
	
    <TextView 
        android:id="@android:id/empty" 
        android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
        android:text="Empty set" 
        android:textColor="#000000" />

</LinearLayout>

I'm getting the background as white, but am not sure where to set the foreground to black. I've tried in the xml and looks like it's not helping.

Android Solutions


Solution 1 - Android

Ok, here are some things that you should be clear about:

  1. The background color you are setting in your xml file is of the activity and not of the ListItems you are trying to define.
  2. Every list item has its own layout file which should be passed or inflated in case you are using complex layout for list item.

I'll try to explain this with a code sample:

**Lets start with ListItems layout **: save it in your res/layout folder of you Android project with say list_black_text.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Definig a container for you List Item-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Defining where should text be placed. You set you text color here-->
    <TextView
        android:id="@+id/list_content"
        android:textColor="#000000"
        android:gravity="center"
        android:text="sample"
        android:layout_margin="4dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

Well, a simple layout with a TextView to be precise. You must have an id assigned to TextView in order to use it.

Now coming to you screen/activity/chief layout, as I said you are defining background to your screen with android:background attribute. I see you have defined a TextView there as well and I suspect you are trying to define content/list item there, which is not at all needed.

Here's your edited layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="#FFFFFF">

    <ListView 
        android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
        <!-- REMOVED TEXT VIEW, AND KEEPING BACKGROUND WHITE -->
</LinearLayout>

And lastly, most importantly, set your adapter.

setListAdapter(new ArrayAdapter<String>(
			this, R.layout.list_black_text, R.id.list_content, listItems));

Notice the layout resource which we are passing to adapter R.layout.list_black_text, and R.id.list_content which is TextView ID we declared. I have also changed ArrayAdapter to String type since it's generic.

I hope this explains everything. Mark my answer accepted if you agree.

Messy but a good quick fix way
You can also do this with a quick fix if you do not want to go ahead with complex layout defining etc.

While instantiating the adapter declare an inner class to do this, here is the code sample:

	ArrayAdapter<String> adapter=new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, listItems){
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view =super.getView(position, convertView, parent);
			
			TextView textView=(TextView) view.findViewById(android.R.id.text1);
			
			/*YOUR CHOICE OF COLOR*/
			textView.setTextColor(Color.BLUE);
			
			return view;
		}
    };
        
    /*SET THE ADAPTER TO LISTVIEW*/
    setListAdapter(adapter);

Solution 2 - Android

  1. Create a styles file, for example: my_styles.xml and save it in res/values.

  2. Add the following code:

     <?xml version="1.0" encoding="utf-8"?>
     <resources>
     
     <style name="ListFont" parent="@android:style/Widget.ListView">
         <item name="android:textColor">#FF0000</item>
         <item name="android:typeface">sans</item>
     </style>
     
     </resources>
    
  3. Add your style to your Activity definition in your AndroidManifest.xml as an android:theme attribute, and assign as value the name of the style you created. For example:

     <activity android:name="your.activityClass" android:theme="@style/ListFont">
    

NOTE: the Widget.ListView comes from here. Also check this.

Solution 3 - Android

I cloned the simple_list_item_1(Alt + Click) and placed the copy on my res/layout folder, renamed it to list_white_text.xml with this contents:

<?xml version="1.0" encoding="utf-8"?>    
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="@color/abc_primary_text_material_dark"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

The android:textColor="@color/abc_primary_text_material_dark" translates to white on my device.

then in the java code:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, myList);

Solution 4 - Android

You have to define the text color in the layout simple_list_item_1 that defines the layout of each of your items.

You set the background color of the LinearLayout and not of the ListView. The background color of the child items of the LinearLayout are transparent by default (in most cases).

And you set the black text color for the TextView that is not part of your ListView. It is an own item (child item of the LinearLayout) here.

Solution 5 - Android

never use getApplicationContext(). Just use your Activity as the Context. See if that helps.

Please check here: CommonsWare answers

Solution 6 - Android

You can do this in your code:

final ListView lv = (ListView) convertView.findViewById(R.id.list_view);

for (int i = 0; i < lv.getChildCount(); i++) {
	((TextView)lv.getChildAt(i)).setTextColor(getResources().getColor(R.color.black));
}

Solution 7 - Android

if you didnot set your activity style it shows you black background .if you want to make changes such as white background, black text of listview then it is difficult process.

ADD android:theme="@style/AppTheme" in Android Manifest.

Solution 8 - Android

I needed to make a ListView with items of different colors. I modified Shardul's method a bit and result in this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, colorString) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = (TextView) super.getView(position, convertView, parent);
                textView.setBackgroundColor(assignBackgroundColor(position));
                textView.setTextColor(assignTextColor(position));
                return textView;
            }
        };
        colorList.setAdapter(adapter);

assignBackgroundColor() and assignTextColor() are methods that assign color you want. They can be replaced with int[] arrays.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarsha M VView Question on Stackoverflow
Solution 1 - AndroidShardulView Answer on Stackoverflow
Solution 2 - Androidsilver_mxView Answer on Stackoverflow
Solution 3 - AndroidalduxView Answer on Stackoverflow
Solution 4 - Androiduser432219View Answer on Stackoverflow
Solution 5 - Androidjohn vuongView Answer on Stackoverflow
Solution 6 - AndroidVijay NagarathinamView Answer on Stackoverflow
Solution 7 - AndroidHaseeb PervaizView Answer on Stackoverflow
Solution 8 - AndroidWakemanView Answer on Stackoverflow