First Span Kit 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 the type is BOLD.

What did I miss?

+4
source share
3 answers

According to the documentation:

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

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

+7
source

You should create an object as shown below.

 ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE); ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); textView.setText(ss) 
0
source

The best solution is to use a static method instead of creating multiple objects:

 CharacterStyle.wrap(CharacterStyle c) 
0
source

Source: https://habr.com/ru/post/1493425/


All Articles