6

I am creating one application in which I am searching for a Perticular word in text view.I have one edit text,one text view and one button,when I am entering any word in edit text and clicking on button it gives me the line position and position of word in that line from entire text file...I have append text file's contain in text view...now my question is can I highlight that all word which is there in text view entered by edit text.?if I can than please tell me how to do it..if any one have idea of it?

Aamirkhan
  • 5,746
  • 10
  • 47
  • 74

4 Answers4

7

You can also do it by using a Spannable, which is convenient as 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 string in which the word to highlight exists, color is the color you want the hightlighted text to have, start is the position of the word and end is where the word ends (start+word.length()).

The SpannableString res can then be applied to your textview like a regular string:

textView.setText(res);

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

Edit: In your case it would be something like this (you will have to save the value of linecount and indexfound for when you read 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);
}
Jave
  • 31,598
  • 14
  • 77
  • 90
  • still not getting clear can you update my code?here is my codehttp://pastebin.com/BCcHWAgS – Aamirkhan Dec 15 '11 at 10:37
  • http://pastebin.com/4g1VkPTy here is my code at line 18 u can see that i i am appying for span at thrree diffrent condition i don't know why it's not working?it's shows out put for the last position only – Aamirkhan Dec 16 '11 at 10:11
  • here is my code http://pastebin.com/4g1VkPTy i don't know why its not working at line 18 you can see that i have given span for three different condition but as a out put it just show for last value – Aamirkhan Dec 16 '11 at 10:14
  • You need to create a new span for every time you want a new position, otherwise you just move the old one. I.e. move the StyleSpan boldSpan = new StyleSpan( Typeface.BOLD ); into your loop. – Jave Dec 16 '11 at 10:15
  • for example my string is aammmmaaammmmmaamaa ok now in this i have aa in three position when i am doing by this code:it's just higlighting first aa what is the solution of it?here is my codehttp://pastebin.com/ydmUHYH4 it just showing first higligted value – Aamirkhan Dec 16 '11 at 10:42
  • I changed Spannable to SpannableString, and just in case, I also called setText() on your textview after the string was updated. You can try now. – Jave Dec 16 '11 at 10:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5909/discussion-between-aamirpathan-and-jave) – Aamirkhan Dec 16 '11 at 10:57
  • hi jave i have one problem that i m not able to set the style of he font by spannable method from my asset folder – Aamirkhan Jan 03 '12 at 10:06
  • hi jave i have one problem that i m not able to set the style of he font by spannable method from my asset folder,here is my code http://pastebin.com/USdYxVQS please help me to solve it – Aamirkhan Jan 03 '12 at 10:12
3

Yes You can highlight some portion of the text view by writing HTML

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
san
  • 1,845
  • 13
  • 23
0

use Spannable to set text in spannable you can setSpan by which you can highlight the text you want

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
0
string ="<font color='#ff0000' > <b>hello</b> </font>"

textview.setText(Html.fromHtml(string)) 

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

jennifer
  • 8,133
  • 22
  • 69
  • 96