7

I have a problem with a SpannableString object.

Below's a short example:

SpannableString spanstr = new SpannableString("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), 0);

SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(spanstr);
sb.append("\n"); // A newline
sb.append("The first line is bold. This one isn't.");

CharSequence cq = sb.subSequence(0, sb.length());
// ^There's no such method to return a SpannableString,
// so I try to return a CharSequence instead.

// And then, at last:
TextView contentView = (TextView) findViewById(R.id.some_id);
contentView.setText(cq);

What the example's trying to do is showing this:

Bold please!
The first line is bold. This one isn't.

But the problem is: the first line of the text won't show up in bold.

Why doesn't it do it expected?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130

5 Answers5

20

Use the spannable string builder for setting as text in textview :

contentView.setText(sb);

or else you can do like this :

SpannableStringBuilder spanstr = new SpannableStringBuilder("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanstr.append("\n");
spanstr.append("The first line is bold. This one isn't.");
contentView.setText(spanstr);
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
2

use SpannableStringBuilder instance itself.

contentView.setText(sb);

output with your code:

enter image description here

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Thank you very much. I called `toString()` on `spannableBuilder` and couldn't understand why I was not seeing a styled text. – Renat Kaitmazov Sep 14 '16 at 09:25
  • 1
    @RenatKaitmazov You should not call toString() method on SpannableBuilder, if you do so it will consider it as String, not a styled text – Pragnani Sep 14 '16 at 14:18
2

Try the below. You need to set the spannable string to the textview. So set the spannable string to your text as below

String s= "The first line is bold. This one isn't";
String title="Bold Please!";  
TextView tv = (TextView) findViewById(R.id.some_id);
tv.setText("");
SpannableString ss1=  new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(s);

I tried the above and you can see the resulting snapshot below.

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

If you use custom font in your device. There is such a silly bug i think. Please change your custom font to default in your device and try again.

Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23
0

late for the answer but for future reader who facing this problem,

make sure you don't call toString() when setting builder into textview

val builder = SpannableStringBuilder()
builder.setspan(xxxxxxx)

do this :

textview.text = builder

and dont do this :

textview.text = builder.toString() //will remove the styling

alas, the styling from SpanableStringBuilder wont work

daya pangestu
  • 400
  • 3
  • 12