marlar
marlar

Reputation: 4145

How do I dynamically change the textColor of my listview items?

I have a listview where the individual items is defined in custom_row_views.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">
  <TextView android:id="@+id/showtitle"
  android:textSize="17sp"
  android:textStyle="bold"
  android:textColor="#FFFF00"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <TextView android:id="@+id/showdate"
  android:textSize="14sp"
  android:textStyle="italic"
  android:textColor="#CCCCCC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <TextView android:id="@+id/showsummary"
  android:textSize="17sp"
  android:textStyle="normal"
  android:textColor="#FFFFFF"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

Note that the three textviews have different text colors.

Now, based on a setting in the preferences, the user should be to change the text color of the textview items.

Basically, I see two ways to do this. One is using a theme:

<resources>
  <style name="ThemeBlack" parent="@android:style/Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:typeface">sans</item>
    <item name="android:background">#999999</item>
    <item name="android:textSize">16sp</item>
  </style>
  <style name="ThemeRed" parent="@android:style/Theme">
    <item name="android:textColor">#0000FF</item>
    <item name="android:typeface">sans</item>
    <item name="android:background">#c81111</item>
    <item name="android:textSize">16sp</item>
  </style>
</resources>

and then in onCreate() for example:

this.setTheme(R.style.ThemeRed);

The problem here is that it changes all the textviews' text colors to whatever is defined in the styles. In other words, they are no longer different. So my first specific questions is:

Is it possible to somehow define or apply the styles so they cater for the listview having three textviews with individual colors?

Another approach is simply setting the text color programmatically, not using styles and themes. This was my first approach and I thought it would be easy, but I have struggled with it for hours to no avail.

I have tried the following in the onCreate of the ListActivity:

TextView tv = (TextView) findViewById(R.id.showsummary);
tv.setTextColor(Color.RED);

but that makes the app crash.

Then I tried this:

TextView tv = null;
LayoutInflater inflater = this.getLayoutInflater();
View aView = inflater.inflate(R.layout.custom_row_view, null);
tv = (TextView) aView.findViewById(R.id.showsummary);
tv.setTextColor(Color.RED);

It doesn't crash, but it doesn't work either!

So my second question is:

How do I change the text color of my listview items in code?

Note that all listview items should have the new color; what is important is that the three individual textviews inside the items should be colored individually. In other words, I am not trying to set the colors of a single item in the listview.

UPDATE: I don't know if it makes any difference, but this is how the listview is popuplated:

Cursor showsCursor = mDbHelper.fetchSummaries(mCategory);
String[] from = new String[]{C2CDbAdapter.SUMMARY_TITLE, C2CDbAdapter.SUMMARY_DATE, C2CDbAdapter.SUMMARY_SUMMARY};
int[] to = new int[]{R.id.showtitle, R.id.showdate, R.id.showsummary};
SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to);
setListAdapter(shows);

Upvotes: 5

Views: 18546

Answers (3)

marlar
marlar

Reputation: 4145

After lots of googling and trial and error, I found a quite simple solution. This example overrides the adapter's getView method by creating an anonymous method, but it is of course also possible to declare a new class based on SimpleCursorAdapter and use that in setListAdapter.

setListAdapter(new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View row = super.getView(position, convertView, parent);
                // Here we get the textview and set the color
                TextView tv = (TextView) row.findViewById(R.id.showsummary);
                tv.setTextColor(Color.YELLOW);
                return row;
        }
});

The nice thing with the anonymous method is that it can be used on any adapter without you having to subclass that particular class.

My coded is inspired on this blog post: http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

Upvotes: 10

Howard Hodson
Howard Hodson

Reputation: 943

Is this what you are asking?

    setContentView(R.layout.main);
    TextView messageText = (TextView) findViewById(R.id.mainTextItem1);
    messageText.setText("This is a test");
    messageText.setTextColor(Color.RED);

The XML world start:

You can select colors with a switch statement. Colors are type int.

int myColor = 0xffff0000; // This is red

int myColor = Color.RED; // and so is this.

messageText.setTextColor(myColor); // now a user can select a color

Upvotes: 1

Houcine
Houcine

Reputation: 24181

try to set a tag to your textView, and then find it by tag , and finally set the color using the method setTextColor(Color.RED);

Upvotes: 0

Related Questions