Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 795
marlar Avatar asked Jul 13 '11 15:07

marlar


People also ask

How do you change the color of text in dynamics?

You can set a font color for text contents dynamically using textColor tags. Syntax of a textColor tag is defined as follows. Note – A textColor tag can be used anywhere in a template document except charts.

How to change text color in Android Studio dynamically?

We can get the reference to TextView widget present in layout file and change the color dynamically with Kotlin code. To set the color to the TextView widget, call setTextColor() method on the TextView widget reference with specific color passed as argument.

How do I change the color of TextView?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.


2 Answers

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

like image 159
marlar Avatar answered Sep 30 '22 10:09

marlar


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

like image 36
Howard Hodson Avatar answered Sep 30 '22 10:09

Howard Hodson