How to dynamically load a color from a resource based on the 1st character of the text

I have a contact list, and I want to change the textcolor of the ListView element based on the 1st character that it has. I took the color for [az] in color.xml and want to set it to my TextView . I took something like this.

My color is .xml

 <color name="a">#ff0000</color> <color name="b">#00ff00</color> <color name="c">#0000ff</color>` 

And I want to do something like this.

 textview1.setTextColor( getResources().getIdentifier("@color/" + textview1.getText().toString().toLowerCase().charAt(0), null, getPackageName())); 

Is there any other way to accomplish this? Can anyone suggest me ... Thanks in Advance ...

+5
source share

All Articles