I have a list in which individual elements are 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 text images have different text colors.
Now, based on the setting in the settings, the user must change the text color of the textview elements.
Basically, I see two ways to do this. One uses the 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 is that it changes all text colors of textviews to everything that is defined in styles. In other words, they no longer differ from each other. Therefore, my first specific questions:
Is there any way to define or apply styles so that they serve a list that has three text images with separate colors?
Another approach is to simply set the text color programmatically without using styles and themes. This was my first approach, and I thought it would be easy, but I struggled with it for several hours to no avail.
I tried the following in onCreate ListActivity:
TextView tv = (TextView) findViewById(R.id.showsummary); tv.setTextColor(Color.RED);
but this causes the application to 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);
This is not a failure, but it does not work either!
So my second question is:
How to change the text color of my list items in code?
Note that all list items must have a new color; The important thing is that three separate text images inside the elements must be individually colored. In other words, I'm not trying to set the colors of a single item in a list.
UPDATE: I don't know if any value matters, but this is how the popuplated listview:
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);