Rémy
Rémy

Reputation: 422

SpannableString and TextAppearanceSpan, apply only color

I want to have 2 text style in one text view, so i trying

Spannable text = new SpannableString(pseudo + " " + "some text after that");
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle), 0, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.TextStyle), pseudo.length() + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

holder.mText.setText(text);

Here you can find my styleS

<style name="PseudoStyle">
  <item name="android:textAllCaps">true</item>
  <item name="android:textSize">14sp</item>
  <item name="android:textStyle">bold</item>
  <item name="android:textColor">@color/pink_light</item>
</style>
<style name="TextStyle">
  <item name="android:textColor">@color/white</item>
</style>

But at all, only android:textColor is apply.

Can you help to have other style with this method?

Thanks

Upvotes: 8

Views: 4872

Answers (2)

R&#233;my
R&#233;my

Reputation: 422

I found a tricky solution, add StyleSpan Bold normally and not by style

java:

text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle), 0, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, pseudo().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.TextStyle), pseudo.length() + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

xml:

<style name="PseudoStyle">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/pink_light</item>
</style>

<style name="TextStyle">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/white</item>
</style>

Upvotes: 6

jantursky
jantursky

Reputation: 1130

Why don't you declare two styles and apply the current styles for the part of the text in SpannableString like this (it's just a example):

Spannable text = new SpannableString(pseudo + " " + "some text after that");
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle_1), 0, pseudo.length() - 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle_2), pseudo.length() - 10, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.mText.setText(text);

Upvotes: 1

Related Questions