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;
}