7

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));
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45

3 Answers3

10

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);
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • it's easy, for each word you want to highlight, you put start index and end index in setSpan method. You can have an array of strings and highlight each word in a loop or make your text an array of words and decide if each word must be highlighted. – Buda Gavril Apr 23 '12 at 11:45
  • yep it is highlighting.if please can you give me a way to find out start index and end index of a word.(i mean logic inside the loop). – Dinesh Anuruddha Apr 23 '12 at 11:55
  • for(String word : words){ int start = test.inxexOf(word); int end = start + word.length(); Spannable WordtoSpan = new SpannableString(bigText); WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(WordtoSpan); – Buda Gavril Apr 23 '12 at 12:56
  • Hello guys , can you please @BudaGavril, try to show me how you turned a TextView into a an Array and you used **words**, in the for loop – Lutaaya Huzaifah Idris Dec 22 '16 at 14:44
  • Coz me i was doing it like this in the searchView , but all the words become Invisible nad only the searched text is the one which appears – Lutaaya Huzaifah Idris Dec 22 '16 at 14:46
4

If I can understand you have the list of words and you want to find this words in text and highlight them so in this answer you have three input parameter:

  1. full text.
  2. yourList
  3. yourTextview to show the result text

    String text = "full of your text";
    Spannable textSpannable = new SpannableString(text);
    
    for (int j =0 ; j<yourList.size() ; j++) {
        //word of your list
        String word = String.valueOf(yourList.get(j));
        //find index of words
        for (int i = -1; (i = text.indexOf(word, i + 1)) != -1; i++) {
            //find the length of word for set color
            int last = i + word.length();
            //set text color with spannable
            textSpannable.setSpan(new BackgroundColorSpan(Color.parseColor("#0cab8f")),
                    i, last, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    yourTextView.setText(textSpannable);
    
nima barati
  • 345
  • 3
  • 8
-1

Just to make it simple, here I post my methode

. . . . . . . .

First getting ready to use the methode

    ArrayList<String> searchWords = new ArrayList<String>(Arrays.asList("Second", "Scottish", "forces", "England"));

    String text = "1333 – Second War of Scottish Independence: The Scottish-held town of Berwick-upon-Tweed surrendered to English forces, ending a siege led by Edward III of England (depicted).";


    TextView sampleTextView = new TextView(currentContext); // currentContext = getContext();

    if (searchWords != null) {
        Spannable newText = setSpanHighlight(text, searchWords);
        sampleTextView.setText(newText, TextView.BufferType.SPANNABLE);
    }
    else{
        sampleTextView.setText(text);
    }

The methode

    private Spannable setSpanHighlight(String text, @NonNull ArrayList<String> searchWord) {
    Spannable newText = new SpannableString(text);

    if (searchWord.size() != 0) {
        for (String word : searchWord){
            if (text.contains(word)){
                int beginIndex = text.indexOf(String.valueOf(word)); //Unnecessary 'String.valueOf()' call => if you have something else than String
                int endIndex = beginIndex + word.length();

                newText.setSpan(
                        new ForegroundColorSpan(Color.BLUE),
                        beginIndex,
                        endIndex,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return newText;
}