Reputation: 11
Perhaps it has been said a lot of times, and I'm sorry, but searching all the web wouldn't help me to find a solution. I have to make a ListView from an array of String, but I want the color of text changing for each line, for example if the string contains "yellow" word the color of the text would be yellow. How can I set a thing like this?
Upvotes: 1
Views: 1103
Reputation: 3111
You can implement a simple adapter like this and use it to fill the ListView
public class MySimpleStringAdapter extends ArrayAdapter<String> {
public MySimpleStringAdapter (Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String myString = getItem(position);
//check your string
//Change the color as you want
//((TextView)convertView).setTextColor();
}
}
On the main class
MySimpleStringAdapter mySimpleNewAdapter = new MySimpleStringAdapter (context,textViewResourceId,stringList);
listView.setAdapter(mySimpleNewAdapter );
Upvotes: 1
Reputation: 1597
Create a HashMap with key as color word and value as integer.
Sample code:
HashMap<String, Integer> colorCode = new HashMap<String, Integer>();
colorCode.put("Red", Color.parseColor("Red"));
// put all pre-defined color in map
tv.setTextColor(colorCode.get("your color word"));
Upvotes: 1
Reputation: 1774
With the last version of Java, you can do switch statment on a String value , so switch on the string and setBackgroundColor in each case
Upvotes: 0