0

I have two typefaces and I would like to use Typeface1 for the titles and Typeface2 for the body.

Is there a way to do that? I can add tags around the title if that would help, like <b>Title</b>

Typeface typeface1 = Typeface.createFromAsset(getAssets(), "fonts/Typeface1.ttf");
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Typeface2.ttf");
textView.setTypeface(typeface1);

textView.setText("<b>Title 1</b>\n" +
                "Body 1\n" +
                "<b>Title 2</b>\n" +
                "Body 2\n" +
                "<b>Title 3</b>\n" +
                "Body 3");
ankita kedia
  • 128
  • 1
  • 9
user3051755
  • 1,497
  • 2
  • 16
  • 27

3 Answers3

2

You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.

Example:

textView.setText(Html.fromHtml("<b>Title</b><br>Body1<br><b>Title2</b><br>Body2"));

This might work

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
1

try using SpannableStringBuilder like provided in this answer

TextView txt = (TextView) findViewById(R.id.custom_fonts);  
Typeface font = Typeface.createFromAsset(getAssets(), "font1.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "font2.ttf");   
SpannableStringBuilder spannableStringBuilder  = new SpannableStringBuilder("TextTextText");
spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font2), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
txt.setText(SS);

CustomTypefaceSpan.java

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
    oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
}
Community
  • 1
  • 1
ColdFire
  • 6,764
  • 6
  • 35
  • 51
0

Its not recommended to mix typefaces and html tags.
To use html tags in the textview, see (Satish Kumar's) answer.

textView.setText("<b>Title 1</b>\n" +
                "Body 1\n" +
                "<b>Title 2</b>\n" +
                "Body 2\n" +
                "<b>Title 3</b>\n" +
                "Body 3");

To use multiple fonts in the same text view,

TextView txt = (TextView) findViewById(R.id.custom_fonts);  
        txt.setTextSize(30);
        Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
        Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");   
        SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
        SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        txt.setText(SS);

CustomTypefaceSpan Class:

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

    public class CustomTypefaceSpan extends TypefaceSpan {
        private final Typeface newType;

        public CustomTypefaceSpan(String family, Typeface type) {
            super(family);
            newType = type;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            applyCustomTypeFace(ds, newType);
        }

        @Override
        public void updateMeasureState(TextPaint paint) {
            applyCustomTypeFace(paint, newType);
        }

        private static void applyCustomTypeFace(Paint paint, Typeface tf) {
            int oldStyle;
            Typeface old = paint.getTypeface();
            if (old == null) {
                oldStyle = 0;
            } else {
                oldStyle = old.getStyle();
            }

            int fake = oldStyle & ~tf.getStyle();
            if ((fake & Typeface.BOLD) != 0) {
                paint.setFakeBoldText(true);
            }

            if ((fake & Typeface.ITALIC) != 0) {
                paint.setTextSkewX(-0.25f);
            }

            paint.setTypeface(tf);
        }
    }


References:
* Multiple TypeFaces
* Span styles and custom typefaces

Community
  • 1
  • 1
Praveen Kishore
  • 436
  • 1
  • 3
  • 14