Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 417
Rémy Avatar asked Jun 08 '18 13:06

Rémy


People also ask

How to use spannable object in Java?

In order to use a span, we need to call setSpan (Object spans, int start, int end, int flag) on the Spannable object. Object span parameter refers to the span to apply to the text, start and end refer to the indexes of the positions of text over which spans are to be applied.

What is the difference between spannedstring and spannablestring?

SpannedString: This is used when there is no need to modify the text or the markup after its creation. SpannableString: This is used when there is no need to modify the text but you need to modify the markup i.e. you need to add some spans to your text.

What is the use of spannablestringbuilder in Android?

SpannableString: This is used when there is no need to modify the text but you need to modify the markup i.e. you need to add some spans to your text. SpannableStringBuilder: This is used when you need to modify the text as well as the markup. This is a quick intro about the Spans in Android. Now, let's see some of the use-cases of it.

How to set a spannable string to textview in Java?

Once we define the string we convert it into a Spannable String after that we define all the spans that we need for modification and then we set these spans over the spannable strings and finally set the spannable string to TextView. Below is the code for the MainActivity.java file.


1 Answers

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>
like image 136
Rémy Avatar answered Oct 16 '22 23:10

Rémy