Text color in Android ListView

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 get the background white, but I don’t know where to set the foreground to black. I tried in xml and it doesn't seem to help.

+42
android listview
Dec 26 '10 at 10:04
source share
7 answers

Well, here are some things you should know about:

  • The background color that you set in your XML file refers to the activity, not to the ListItems that you are trying to define.
  • Each list item has its own layout file, which must be transferred or inflated if you use a complex layout for the list item.

I will try to explain this with a sample code:

**** Let's start with the ListItems ** layout : save it in your res/layout folder of your Android project, 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 TextView , to be precise. You must have an identifier assigned by TextView in order to use it.

Now the screen / activity / main layout will come to you, as I said that you define the background on the screen with the android:background attribute. I see that you also defined a TextView, and I suspect that you are trying to define there a content / list item that is not needed at all.

Here's the 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 finally, most importantly, install the adapter.

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

Pay attention to the layout resource that we pass to the adapter R.layout.list_black_text , and R.id.list_content , which we declared the ID TextView. I also changed the ArrayAdapter to a String type, as it is generic.

Hope this all explains. Mark my answer if you agree.

A useless but quick way to quickly fix
You can also do this with a quick fix if you don't want to go ahead with defining a complex layout, etc.

While the adapter instance declares an inner class for this, here is a sample code:

  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); 
+134
Dec 26 '10 at 10:25
source share
  • Create a stylesheet, for example: my_styles.xml and save it in res/values .
  • 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> 
  • Add your style to the Activity definition in your AndroidManifest.xml as the android:theme attribute and set the value to the name of the style you created. For example:

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



NOTE: Widget.ListView comes from here . Also check this one out .

+21
Jun 21 '11 at 12:20
source share

I cloned simple_list_item_1 (Alt + Click) and put a copy in my res/layout folder, renamed it to list_white_text.xml with this content:

 <?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" /> 

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

then in java code:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, myList); 
+6
Aug 27 '15 at 11:41
source share

You must define the color of the text in the layout * simple_list_item_1 *, which defines the layout of each of your elements.

You specify the background color of LinearLayout, not ListView. The background color of LinearLayout children is transparent by default (in most cases).

And you set the black color of the text for the TextView, which is not part of your ListView. This is a separate element (a child of LinearLayout) here.

+3
Dec 26 '10 at 10:12
source share

never use getApplicationContext() . Just use your activity as a context. See if that helps.

Please check here: CommonsWare Answers

0
Nov 24 '14 at 8:09
source share

If you have not set your style of activity, it shows a black background. If you want to make changes such as white background, black text of the list, then this is a complex process.

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

0
Dec 24 '17 at 5:27
source share

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)); } 
-2
Dec 16 '16 at 0:40
source share



All Articles