0

I want to bold multiple words from textview's text. I am having words in array that i want to bold in textview's text. how to do it by SpannableStringBuilder??

My array:

string[] array = {"Life","happiness","sadness","tears","smiles","laughter","emotions"};

My Textview text:

Life is filled with happiness, sadness, tears, smiles, laughter and other emotions but when life gets you down, just be strong about it and keep your head up high and have faith in all things in life.

I go through link and other but not found any solution which i want.

    TextView text = (TextView) findViewById(R.id.textView1);
    String[] list = new String[] { "Life","happiness","sadness","tears","smiles","laughter","emotions"};    

    String textData ="Life is filled with happiness, sadness, tears, smiles, laughter and other emotions but when life gets you down, just be strong about it and keep your head up high and have faith in all things in life";
    CharSequence cseq = "";
    for(int i = 0 ; i < list.length ; i++)
    {
          cseq = setSpanBetweenTokens(textData, list[i] , new StyleSpan(Typeface.BOLD_ITALIC));
    }
    text.setText(cseq.toString()); 


public static CharSequence setSpanBetweenTokens(CharSequence text,
            String token, CharacterStyle... cs)
        {
            // Start and end refer to the points where the span will apply
            int tokenLen = token.length();
            int start = text.toString().indexOf(token) + tokenLen;
            int end = text.toString().indexOf(token, start);

            if (start > -1 && end > -1)
            {
                // Copy the spannable string to a mutable spannable string
                SpannableStringBuilder ssb = new SpannableStringBuilder(text);
                for (CharacterStyle c : cs)
                    ssb.setSpan(c, start, end, 0);

                // Delete the tokens before and after the span
                ssb.delete(end, end + tokenLen);
                ssb.delete(start - tokenLen, start);

                text = ssb;
            }

            return text;
        }
Community
  • 1
  • 1
Smily
  • 2,646
  • 4
  • 23
  • 31

3 Answers3

2

try as using Html.fromHtml:

string[] array = {"Life","happiness","sadness",
"tears","smiles","laughter","emotions"};

String strtemp="";
    for(int i=0;i<array.length-2;i++){
     if(i<array.length-3)
       strtemp+="<b>"+array[i]+",<b/>";
      else{
       strtemp+="<b>"+array[i]+"<b/>";
     }
    }
    txtview=(TextView)findViewById(R.id.selection);
    txtview.setText(Html.fromHtml("YOUR_TEXT HERE"+strtemp+
             "YOUR_TEXT HERE <b>"+array[array.length-1]
                     +"</b> YOUR_TEXT HERE"));
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

If I understood your question correctly, you have a TextView that has some dynamic text in it, and you want to bold certain words in it. Try something like this:

Spannable sb = new SpannableString(textFromTextView);
for(int i = 0 ; i < array.length ; i++) {
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
        textFromTextView.indexOf(array[i]), 
        array[i].length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

then call the setText() function of your textview with sb as parameter.

1615903
  • 32,635
  • 12
  • 70
  • 99
1

Please use the below code:

 textView.setText(Html.fromHtml("<b>"+array[0]+"</b>"+ "is filled with "+"<b>"+array[1]+"</b>"+","+"<b>"+array[2]+"</b>"+","+"<b>"+array[3]+"</b>"+","+"<b>"+array[4]+"</b>"+","+"<b>"+array[5]+"</b>"+"and other"+"<b>"+array[6]+"</b>"+"but when life gets you down, just be strong about it and keep your head up high and have faith in all things in life."));
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • I dont want as you suggest me I want like iterate loop till array length and if that word found in that will formated like wise.. Understand ?? – Smily Dec 21 '12 at 07:56
  • then place the above line in if condition - check till the array.length() and array[i]!=null.... – Avadhani Y Dec 21 '12 at 08:15