0

I've got problems to set StyleSpan and ForegroundColorSpan in the same string. Here's my code:

SpannableStringBuilder text_1_2 = new SpannableStringBuilder(getString(R.string.why_text_1_2));

StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
ForegroundColorSpan fcs = new ForegroundColorSpan(getResources().getColor(R.color.custom_blue));

text_1_2.setSpan(fcs, 0 , text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
text_1_2.setSpan(bss, 0, text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

The resulting string has blue color but is not bold.

Thanks.

Tofasio
  • 425
  • 3
  • 14

1 Answers1

5

Can you try this

text_1_2.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0 , text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
text_1_2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.custom_blue)), 0, text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

But my recommendation is if you don't want to style a part of the string apply the style and color using

 android:textColor="@color/color_name"
 android:textStyle="bold"
Fahim
  • 12,198
  • 5
  • 39
  • 57
  • The problem is have to use 3 different styles on same text, and i'm using `TextUtils.concat()` for the 3 different Strings. The first code you put works for me. Thanks for editing the answer. – Tofasio Feb 24 '15 at 12:16
  • great.. then 1st option should be fine – Fahim Feb 24 '15 at 12:18