Highlighting a particular word in a text view

I am creating one application in which I am looking for a pertext word in a text view. I have one edit text, one text view and one button when I enter any word in a text editor and press a button that gives me the position of the line and the position of the word in this line from the whole text file ... I have an append text file contained in the text view ... now my question is: can I select the whole word that is in the text view, entered by editing the text. "If I can't, please tell me how to do this. If anyone has an idea about this?

+6
source share
4 answers

You can also do this using Spannable , which is convenient since you know the position of the word:

SpannableString res = new SpannableString(entireString); res.setSpan(new ForegroundColorSpan(color), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); 

Where entireString is the line where the highlighted word exists, color is the color you need to have tanned text, start is the position of the word, and end is where the word ends (start + word.length ()).

Then SpannableString res can be applied to your texture as a regular string:

 textView.setText(res); 

Note. If you want the background of the text to get color rather than the text itself, use BackgroundColorSpan instead of ForegroundColorSpan .

Edit: In your case, it will be something like this (you will need to save the value of linecount and indexfound when reading the entire text):

 for(String test="", int currentLine=0; test!=null; test=br2.readLine(), currentLine++){ if(currentLine==linecount){ SpannableString res = new SpannableString(test); res.setSpan(new ForegroundColorSpan(0xFFFF0000), indexfound, indexfound+textword.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); } tv.append("\n"+" "+test); } 
+7
source

Yes, you can highlight some part of the text view by writing HTML

 String styledText = "This is <font color='red'>simple</font>."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); 
+3
source

use spannable to set the text to spannable, you can set the span with which you can select the text you want

0
source
 string ="<font color='#ff0000' > <b>hello</b> </font>" textview.setText(Html.fromHtml(string)) 

http://developer.android.com/reference/android/text/Html.html

0
source

Source: https://habr.com/ru/post/903831/


All Articles