1

I have a simple listview that is created with an Array Adapter and ArrayList;

Is there anyway to to access a certain row in the the list view and then change the text color of the text view that resides in that row in the list view?

I know how to change the text color of a textview but i'm having problems accessing the text view that is inside of the list view

au789
  • 5,255
  • 5
  • 20
  • 21

2 Answers2

13

If you look at the source for simple_list_item_1, you'll see that it is just a TextView. The source is in:

<sdk-dir>/platforms/<your-platform>/data/res/layout/simple_list_item_1

The ArrayAdapter superclass will return that TextView in its getView method. That means you can subclass ArrayAdapter, and inside your subclass' getView method, you can simply chain to the superclass, cast the View it returns to TextView, and do your thing. For example, if you wanted to set the first three items in your list to textSize 24 and the rest to 14, you could do the following:

public View getView(int position, View convertView, ViewGroup parent) {
  TextView tv = (TextView) super.getView(position, convertView, parent);

  if (position < 3) {
    tv.setTextSize(24.0f);
  } else {
    tv.setTextSize(14.0f);
  }
  return tv;
}

If you are using a more complicated View than simple_list_item_1, you can figure out the id's of the elements on the View by examining the source and then call findViewById on the View that is returned by the superclass. For example, two_line_list_item.xml has TextViews with ids of android.R.id.text1 and android.R.id.text2, so you should be able to get a handle on them as follows:

public View getView(int position, View convertView, ViewGroup parent) {
  View v = super.getView(position, convertView, parent);
  TextView tv1 = (TextView)v.findViewById(android.R.id.text1);
  TextView tv2 = (TextView)v.findViewById(android.R.id.text2);

  //do what you want with the TextViews
}
Raiv
  • 5,731
  • 1
  • 33
  • 51
Brian Cooley
  • 11,622
  • 4
  • 40
  • 39
1

With a custom list item, in your adapters getView method you could change the text color easily by calling findViewById(R.id.myText) and then calling setTextColor. In fact you can do this with the built in list items, you'd just need to know the ID of the TextView... which I don't offhand but you should be able to find it easily enough.

For changing it in XML see Applying Styles and Themes to change the text color if you are using a standard ListView item.

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • So there is no way to do it in the java file? i was going to change it dynamically based on some input values – au789 Mar 23 '11 at 01:25
  • With a custom list item, in your adapters `getView` method you could change the text color easily by calling `findViewById(R.id.myText)` and then calling `setTextColor`. In fact you can do this with the built in list items, you'd just need to know the `ID` of the TextView. – Joseph Earl Mar 23 '11 at 01:29
  • so you are saying it is not possible with the array list and array adapter. if i want to achieve the results i would need to make a custom list item? – au789 Mar 23 '11 at 01:32
  • So I said you could do this, you'd just need to the id of the textbox which Brian has provided below. – Joseph Earl Mar 23 '11 at 08:43