Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a word in a string and highlight word in a text view in android?

in my android application i have a string that contains a specific word so i want to display whole string in text view and the specific word should be highlighted.Hope following image will give you an idea.

enter image description here

I have used following code to do this but its not working.

CODE:

con is my string and groupNameContent is the text field.

con.replaceAll(arrGroupelements[groupPosition][5],"<font color='#CA278C'>"+arrGroupelements[groupPosition][5]+"</font>.");
groupNameContent.setText(Html.fromHtml(con));
like image 744
Dinesh Anuruddha Avatar asked Apr 23 '12 11:04

Dinesh Anuruddha


People also ask

How do you highlight text on Android?

To highlight text, touch and hold, then drag your finger.

How to increase TextView size in Android Studio?

Go to File -> Settings, a new setting dialogue box will appear. Then go to Editor -> General. Now mark the checkbox Change font size with Ctrl + Mouse wheel and click on Apply button. Now to change your editor font size, you just have to press and hold Ctrl and rotate the Mouse wheel.


1 Answers

for each word, you can use:

TextView textView = (TextView)findViewById(R.id.mytextview01);
//use a loop to change text color
Spannable WordtoSpan = new SpannableString("partial colored text");        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
like image 55
Buda Gavril Avatar answered Sep 23 '22 05:09

Buda Gavril