Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The first setSpan didn't work

final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
final SpannableString ss = new SpannableString("How to " + text + " in " + type);
ss.setSpan(bss, 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(bss, 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);

I want to make text and type BOLD. But only type is BOLD.

What did I miss?

like image 544
user1537779 Avatar asked Feb 15 '23 19:02

user1537779


1 Answers

According to the documentation :

setSpan(Object what, int start, int end, int flags) Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

A StyleSpan can only be used once in a Spannable. You need create a StyleSpan for each text and type

like image 102
njzk2 Avatar answered Feb 24 '23 13:02

njzk2