Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android string colors

Is it possible to change the color of some of the characters in a string in Android? For example change the color of the letters d and r in the word android to green.

<TextView
        android:id="@+id/word"
        android:gravity="center_vertical"
        android:typeface="monospace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30pt"/>
like image 947
zataar Avatar asked Apr 20 '26 02:04

zataar


1 Answers

You could initialize the TextView with html instead of plain text. The html could define the text spans with colors. See Set TextView text from html-formatted string resource in XML for how.

In case you don't want to use html you can use SpannableString.

like image 55
Code Poet Avatar answered Apr 23 '26 01:04

Code Poet