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
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With