TextView with different textSize

70,242

Solution 1

Try

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Solution 2

I know very late to answer but people are still might have the same question.Even I struggled a lot on this. Suppose you have these two strings inside your strings.xml file

 <string name="my_text">You will need a to complete this assembly</string>
 <string name="text_sub1">screwdriver, hammer, and measuring tape</string>

You now need to define two Style for them inside your style.xml with different textSize

<style name="style0">
    <item name="android:textSize">19sp</item>
    <item name="android:textColor">@color/standout_text</item>
    <item name="android:textStyle">bold</item>
</style>
<style name="style1">
    <item name="android:textSize">23sp</item>
    <item name="android:textColor">@color/standout_light_text</item>
    <item name="android:textStyle">italic</item>
</style>

Now from your Java file you need to use the spannable to load these two style and strings on single textView

SpannableString formattedSpan = formatStyles(getString(R.string.my_text), getString(R.string.text_sub0), R.style.style0, getString(R.string.main_text_sub1), R.style.style1);
textView.setText(formattedSpan, TextView.BufferType.SPANNABLE);

Below is the formatStyles method which will return the formatted string after applying the style

private SpannableString formatStyles(String value, String sub0, int style0, String sub1, int style1)
{ 
    String tag0 = "{0}";
    int startLocation0 = value.indexOf(tag0);
    value = value.replace(tag0, sub0);

    String tag1 = "{1}";
    int startLocation1 = value.indexOf(tag1);
    if (sub1 != null && !sub1.equals(""))
    { 
        value = value.replace(tag1, sub1);
    } 
    SpannableString styledText = new SpannableString(value);
    styledText.setSpan(new TextAppearanceSpan(getActivity(), style0), startLocation0, startLocation0 + sub0.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (sub1 != null && !sub1.equals(""))
    { 
        styledText.setSpan(new TextAppearanceSpan(getActivity(), style1), startLocation1, startLocation1 + sub1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } 

    return styledText;
}

Solution 3

Try with AbsoluteSizeSpan

snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

The complete code is:

SpannableStringBuilder snackbarText = new SpannableStringBuilder();
snackbarText.append("Your text");
snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Snackbar.make(getCurrentFocus(), snackbarText, Snackbar.LENGTH_LONG).setAction("Action", null).show();`
Share:
70,242
woyaru
Author by

woyaru

Updated on December 12, 2020

Comments

  • woyaru
    woyaru over 3 years

    Is this possible to set different textSize in one TextView? I know that I can change text style using:

    TextView textView = (TextView) findViewById(R.id.textView);
    Spannable span = new SpannableString(textView.getText());
    span.setSpan(arg0, 1, 10, arg3);
    textView.setText(span)
    

    I know the range start...end of text I want to change size. But what can I use as arg0 and arg3?

  • woyaru
    woyaru over 12 years
    It looks properly but doesn't work. The whole text in my TextView has the same size all the time.
  • woyaru
    woyaru over 12 years
    Hmm maybe I should apply (update) the span to my TextView after setSpan()?
  • woyaru
    woyaru over 12 years
    Sorry for my 3 comments but I have resolved this problem. Just: textView.setText(span).
  • Ionut Negru
    Ionut Negru almost 10 years
    do you know how can I combine size with color for span ?