简体   繁体   中英

How to set color for string in android

I am having a webview . I am just retrieving the webview's content in a string and displaying it in textview . In onTouch event the text will read aloud. Now i want to highlight the text word by word in onTouch event which the text is speaking. For textview we can use setTextColor . But how to setTextColor for string?

Use a SpannableString , for example:

final String s = editText.getText().toString();
final Spannable spannable = new SpannableString(s);
spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Update the endIndex to point to the end of whatever word is being spoken,

Just use like this too

String name = "Test String"; 
textView.setText( Html.fromHtml(String.format("<font color=\"#%s\">text</font>", name ), TextView.BufferType.SPANNABLE));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM