Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set different font and color to part of a TextView

I tried this:

String s = "Some big string"
SpannableStringBuilder sb = new SpannableStringBuilder(s);
//normal font for 1st 9 chars
sb.setSpan(robotoRegular, 0,9,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//bold font for rest of the chars
sb.setSpan(robotoBold, 9,s.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//also change color for rest of the chars
sb.setSpan(new ForegroundColorSpan(Color.BLACK), 9,s.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);

But this didn't work.

It only takes the latest setSpan, ie.., the Text color is being changed but not the font.

like image 358
Archie.bpgc Avatar asked Nov 27 '12 06:11

Archie.bpgc


People also ask

How can I change the color of a part of a TextView in android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How do I add color to TextView?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.

What attribute changes the color of TextView?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .


2 Answers

You have to use a TypefaceSpan instead of a Typeface.

But since you are using a custom typeface you need to extend TypefaceSpan.

Check out this answer and create CustomTypefaceSpan class.

Now do the following:

Typeface robotoRegular = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Regular.ttf");
Typeface robotoBold = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");

TypefaceSpan robotoRegularSpan = new CustomTypefaceSpan("", robotoRegular);
TypefaceSpan robotoBoldSpan = new CustomTypefaceSpan("", robotoBold);

// normal font for 1st 9 chars
sb.setSpan(robotoRegularSpan, 0, 9, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// bold font for rest of the chars
sb.setSpan(robotoBoldSpan, 9, s.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// also change color for rest of the chars
sb.setSpan(new ForegroundColorSpan(Color.BLUE), 9, s.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
like image 139
Benito Bertoli Avatar answered Oct 02 '22 08:10

Benito Bertoli


For peaple who prefer to make utils classes for this:

public static SpannableStringBuilder makeTextBold (Activity activity, String string, int fromCharIndex, int toCharIndex) {
    return makeTextBold(activity, new SpannableStringBuilder(string), fromCharIndex, toCharIndex);
}

public static SpannableStringBuilder makeTextBold (Activity activity, SpannableStringBuilder string, int fromCharIndex, int toCharIndex) {
    SpannableStringBuilder sb = new SpannableStringBuilder(string);
    Typeface bold = Typeface.createFromAsset(activity.getAssets(), "fonts/NexaBold.ttf");
    TypefaceSpan robotoBoldSpan = new CustomTypefaceSpan("", bold);
    sb.setSpan(robotoBoldSpan, fromCharIndex, toCharIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return sb;
}

public static SpannableStringBuilder colorText (int resourceId, String string, Activity activity, int fromCharIndex, int toCharIndex) {
    return colorText(resourceId, new SpannableStringBuilder(string), activity, fromCharIndex, toCharIndex);
}

public static SpannableStringBuilder colorText (int resourceId, SpannableStringBuilder sb, Activity activity, int fromCharIndex, int toCharIndex) {
    sb.setSpan(new ForegroundColorSpan(activity.getResources().getColor(resourceId)), fromCharIndex, toCharIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return sb;
}
like image 45
jobbert Avatar answered Oct 02 '22 09:10

jobbert