I am trying to change the color of certain words (Search results) in a TextView
? I tried to use ANSI colors like this:
text.setText("\u001B31;1m" + "someText");
but it did not work. How do I achieve this?
this will help u
Spannable WordtoSpan = new SpannableString("I know just how to whisper");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(WordtoSpan);
You're looking for TextAppearanceSpan
and 'SpannableString'
To be more clear workflow is as follow
You can also use HTML like in the following example:
string = "<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>";
textView.setText(Html.fromHtml(string));
Without using additional variable (one-line solution):
textView.setText(Html.fromHtml("<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>"));
It is quite easy and understandable solution :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With