Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpannableString: Is it possible to apply two or more RelativeSizeSpans?

I am trying to construct a SpannableString such that it looks like this:

enter image description here

Two characters (m, s) should be smaller than the rest. I have tried to hold all the text in one SpannableString, and I also tried to concatenate two SpannableStrings via a SpannableStringBuilder. The code for one Spannable looks like this:

spannable.setSpan(new RelativeSizeSpan(0.75f), spannable.length() - 1, spannable.length(), 0); 

However, only one formatting is applied - when using the SpannableStringBuilder, only the "m" is smaller, and when using one SpannableString for the whole text, only the "s" is smaller.

Debugging also showed that Spannables seem to hold only one instance of RelativeSizeSpan, meaning there can be only one Span of one type. Is this true or expected behaviour? Would it be advisable to concatenate TextViews instead?

EDIT: By the way, I am trying to remove a HTML.fromHtml() call here for performance reasons (many GC calls).

like image 473
manmal Avatar asked Oct 21 '11 15:10

manmal


1 Answers

If you're still looking for an answer, I might have a solution. I had similar problems. I used TextUtils to concat the 2 SpannableString.

Here is some example code:

SpannableString span1 = new SpannableString("32m"); SpannableString span2 = new SpannableString("50s");  span1.setSpan(new RelativeSizeSpan(0.75f),  2, 3, 0); span2.setSpan(new RelativeSizeSpan(0.75f),  2, 3, 0);  mTextView.setText(TextUtils.concat(span1," " ,span2)); 
like image 159
Norman Avatar answered Sep 19 '22 09:09

Norman