90

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?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
woyaru
  • 5,544
  • 13
  • 54
  • 92

3 Answers3

201

Try

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Michele
  • 6,126
  • 2
  • 41
  • 45
24

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;
}
Saurabh
  • 975
  • 2
  • 12
  • 27
14

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();`