Android ListView Text Color
Ok, here are some things that you should be clear about:
- The background color you are setting in your xml file is of the activity and not of the ListItems you are trying to define.
- 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);
- Create a styles file, for example:
my_styles.xml
and save it inres/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 your
Activity
definition in yourAndroidManifest.xml
as anandroid: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.