Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of a specific character in a string in java / android?

Tags:

java

android

I have a String.

String text= //Some String That I know
String textToBeColored = //Some Letter User enters at run time

How can I change the color of a specific letter that user enters at run time in my String.

like image 566
BST Kaal Avatar asked Jan 30 '26 06:01

BST Kaal


1 Answers

I assume that you want something like particular text that user selects to be highlighted in the string. Below should help you do it.

String text = "abcada";
String textToBeColored = "a";

String htmlText = text.replace(textToBeColored,"<font color='#c5c5c5'>"+textToBeColored +"</font>");
// Only letter a would be displayed in a different color.
txtView.setText(Html.fromHtml(htmlText);
like image 101
Nishanthi Grashia Avatar answered Jan 31 '26 22:01

Nishanthi Grashia