Change text color in android.R.layout.simple_list_item_2

I use a simple adapter to display my code. Unfortunately, I need to change the top color of the textView.

This is a piece of code:

// Keys used in Hashmap String[] from = { "txt1", "txt2" }; // Ids of views in listview_layout int[] ids = { android.R.id.text1, android.R.id.text2 }; SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_2, from, ids); setListAdapter(adapter); 

I tried to create my own simple_list_item_2, but for some reason it will not let me change the color of the textView in xml. Any ideas on how to do this?

My last thought:

findViewById(android.R.id.text1).setTextColor(#000) , but I don't know where to put it, and my hex code is not working.

+8
java android xml simpleadapter
source share
4 answers

you need to override getView from SimpleAdapter. For example:

 SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_2, from, ids) { public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView text1 = (TextView) view.findViewById(android.R.id.text1); text1.setTextColor(Color.RED); return view; }; }; 
+16
source share

Create your own xml layout for your ListView elements and set the color of the TextView text using the textColor attribute:

 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textColor="#ff0000" /> 
+1
source share

If you use the Spinner drop-down list, the text color will not change. To change, we must also add the method described above with the getDropDownView method.

 public View getDropDownView (int position, View convertView, ViewGroup parent) {                View view = super.getDropDownView (position, convertView, parent);                TextView text = (TextView) view.findViewById (android.R.id.text1);                text.setTextColor (Color.BLACK);                return view;            } 
0
source share

You should use setTextColor(Color.any color);

 TextView txt = (TextView) view.findViewById(R.id.text1); txt.setTextColor(Color.yellow); 
-one
source share

All Articles