Colored text can be created by passing the text and a font color name to the following function:
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
The colored text can then be set to a TextView
(or even to a Button
, EditText
, etc.) by using the example code below.
First, define a TextView
as follows:
TextView txtView = (TextView)findViewById(R.id.txtView);
Then, create differently colored text and assign it to strings:
String name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");
Finally, set the two differently colored strings to the TextView
:
txtView.setText(Html.fromHtml(name+" "+surName));
Reference screenshot: