2

I have two spannable object for same string and both have different styles. I need to merge all spannable objects and display all styles into TextView.

From one of returning from Html.fromHtml() and second return StyleSpan(Bold).

I tried TextUtils.concate(htmlSpan, boldSpan) but it displayed same string two times because it append spannable, i want to display my all styles into single string.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Krunal Shah
  • 1,438
  • 1
  • 17
  • 29

2 Answers2

2

Use SpannableStringBuilder but you can create spans inside the append method

SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(String.valueOf(Html.fromHtml(stringToConvert));
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

And then just set sb to your view

textView.setText(sb);

EDIT:

I saw your edited question, Above I rewrote this code to get it to apply two spannables on one string. I think you cannot merge different spannable objects on one string. I believe a workaround would be to apply one span, then convert in to string and then apply the second span. This way it will apply two spans to one string. Or you can manage your bold text type via xml if you always need it in bold

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • i already told i don't want to append string, want to merge my two spannable object which applying on same string but both have different style. – Krunal Shah Mar 20 '18 at 09:03
0

Finally i found my solution, Html.fromHtml() not have spannable type parameter, so firstly i got SpannableStringBuilder from Html.fromHtml() and carry forward it into create Bold StyleSpannable method then set into TextView, and it works awesome.

It retain HTML spannable into bold StyleSpannable method.

Thanks guys for your feed back and response of my answer.

SpannableStringBuilder spannable = setTextViewHTML(content);
common.convertBoldString(spannable);

textView.setText(spannable);

For the above code content is a variable, which contain words which i like to bold and it also contain HTML tags.

convertBoldString is method in my Common methods class which has parameter SpannableStringBuilder and this object use to set bold StyleSpan.

Krunal Shah
  • 1,438
  • 1
  • 17
  • 29