I have some text in a textView
and I want all phrases that are as the Note
in my text select them and change the their color to the red color. How can I do this? Is there a method for this?
Asked
Active
Viewed 65 times
0

android
- 5
- 2
-
http://stackoverflow.com/questions/32778859/find-next-or-previous-word-on-textview/32780509#32780509 – penta Sep 25 '15 at 20:25
-
You can check answers in this [question](http://stackoverflow.com/questions/8612652/select-a-word-on-a-tap-in-textview-edittext). – abozaid Sep 25 '15 at 21:09
2 Answers
0
try this
String text = "Some text in the text view.";
String searchText = "the";
int startPos = -1, endPos = -1;
if(text.toLowerCase().contains(searchText.toLowerCase())){
startPos = text.toLowerCase().indexOf(searchText.toLowerCase());
endPos = startPos + searchText.length();
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
if (startPos != -1){
spanText.setSpan(new ForegroundColorSpan(Color.RED), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(spanText);
}
}

Deepak Goyal
- 4,747
- 2
- 21
- 46
0
You can do the following to highlight all the "Note" words in your textview.. first set the word to change color:
String stringtoChange = "Note";
then change the color using the replaceAll method of the String class:
textToHighlight = textToHighlight.replaceAll(stringtoChange, "<font color='red'>" + stringtoChange + "</font>");
where textToHighlight is your textView text and finally set the edited text to the textView:
your textView.setText(Html.fromHtml(textToHighlight));

tsiro
- 2,323
- 3
- 24
- 46