13

I am trying to change the text color of the ListView but I cant find how to do that..<

    listViewObject= (ListView) findViewById(R.id.chathlist);        
    ArrayList<String> chatHistory = new ArrayList<String>();
    chatHistory.add("Msg 1");
    chatHistory.add("Msg 2");
    chatHistory.add("Msg 3");
    chatHistory.add("Msg 4");
    chatHistory.add("Msg 5");
    chatHistory.add("Msg 6");
    chatHistory.add("Msg 7");
    ArrayAdapter aa=new ArrayAdapter<String>(getApplicationContext(),
    android.R.layout.simple_list_item_1, chatHistory);
    listViewObject.setAdapter(aa);
    listViewObject.invalidate();

I have tried many ways but I can't change the color. It maybe a simple thing but I'm fighting with this.

My XML file is

  <RelativeLayout android:id="@+id/chat_history_container"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            <ListView android:paddingTop="15dip" android:paddingBottom="15dip"
                android:layout_width="fill_parent" android:paddingLeft="15dip"
                android:layout_height="wrap_content" android:id="@+id/chathlist"
                android:layout_gravity="center_horizontal"
                android:layout_centerInParent="true" android:paddingRight="15dip"
                 android:textColor="#FF0000"  android:listSelector="@drawable/gradient_box" >
            </ListView>
  </RelativeLayout>
daksh21ubuntu
  • 276
  • 2
  • 4
  • 14
Hussain
  • 5,552
  • 4
  • 40
  • 50

2 Answers2

11

Check these for more info:

http://www.anddev.org/view-layout-resource-problems-f27/changing-listview-text-color-t14527.html

Changing text color of list view in android

Change Text Color in ListView

Make a layout for your List items and bind that to a ListAdapter.

Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
6

The problem is that you are using the default list item layout "android.R.layout.simple_list_item_1". If you want to change the color of the text in the listview you must create your own list item layout with the correct text color.

You could add this item to your layout folder:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/text1"
        style="?android:attr/listItemFirstLineStyle"
        android:paddingTop="2dip"
        android:paddingBottom="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:textColor="#FF0000" /> 

Then pass this item instead of android.R.layout.simple_list_item_1

Twobard
  • 2,553
  • 1
  • 24
  • 27
  • This of course is useless if your listview is set for singleChoice to display RadioButtons. Writing your own custom layout then requires developing your own adapter and a lot more code. You solution is fine if all the list does is display nothing but text items without RadioButtons. – Johann Jan 07 '13 at 09:19