-1

In android I want to apply a color to my ListView.

Here is the ListView:

listView = (ListView) findViewById (R.id.listView1)

and my adapter to display the list:

adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,
ArrayofName);
listView.setAdapter (adapter);

My variable that contains the color: int textColor. How I could do it? For example you can change the background of the list like this:

listView.setBackgroundColor(textColor); 

But I want to change the color of text.

fredtantini
  • 15,966
  • 8
  • 49
  • 55
user103671
  • 11
  • 4
  • This link may help [Apply Color to Listview](http://stackoverflow.com/questions/4533440/android-listview-text-color) – AnishIsBack Jan 06 '15 at 08:41

3 Answers3

0

you are using:

android.R.layout.simple_list_item_1

create a new XML file your_custom_layout.xml with a textview with ID mytextview1 and set the color of text in the XML as

android:textColor="#ff0000"

and then set your adapter like this:

setListAdapter(new ArrayAdapter<String>(this, R.layout.your_custom_layout, R.id.mytextview1, ArrayofName));
  • In fact, the user can choose the color he wants in a dialogBox with ColorPicker and when he clicked ok, this color goes to the the textColor variable which I want to apply to my listview. That's why I think I can not use an xml( – user103671 Jan 06 '15 at 08:52
0

You would need to use custom TextView xml for changing text color in ListView item.

Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.

list_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:textColor="@color/mycolor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Change your code to use this textview instead of simple_list_item_1 like this:

adapter = new ArrayAdapter <String> (this, android.R.layout.list_text,
ArrayofName);
listView.setAdapter (adapter);

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thanks, but the thing is that the user himself can choose any color he wants and this color is contained in the textColor variablve. I just need to apply this color to the listview. – user103671 Jan 06 '15 at 08:55
  • Then you will have to make custom adapter for your listview. Refer Sonu Raj's answer – MysticMagicϡ Jan 06 '15 at 08:56
  • 1
    Thanks a lot))))))))))))))))) It works)!!!!!!!!!!!!!!!!!!! It makes me feel happy!!!)))))))))))))))) – user103671 Jan 06 '15 at 09:29
0

do it like this

dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, allData) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text = (TextView) view
                .findViewById(android.R.id.text1);

        text.setTextColor(Color.parseColor("#FBB117"));

        return view;
    }
};
Sonu Raj
  • 369
  • 3
  • 11