Vinay W
Vinay W

Reputation: 10190

set custom font through xml

how can i set a font, whose ttf resides in my assets folder through xml? I know how to do that programmatically but how can you do that via xml? Thanks in advance.

Upvotes: 1

Views: 6780

Answers (5)

Snehil Wani
Snehil Wani

Reputation: 41

You can include custom fonts under assets/fonts and then using the code from https://github.com/browep/AndroidCustomFontWidgets/

you can specify the font in your XML, e.g.

<com.github.browep.customfonts.view.FontableTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is in a custom font"
        app:font="MyFont-Bold.otf" />

The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

Upvotes: 0

browep
browep

Reputation: 5107

I have created a library to solve this problem here: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html

You extend a TextView and set the name of the typeface in the xml. There is a downloadable sample.

Upvotes: 1

K_Anas
K_Anas

Reputation: 31466

You can do this by writing your custom TextView, otherwise you will have to set it programmatically. For more details see this link

Upvotes: 1

Raghav
Raghav

Reputation: 4638

You cannot do it using XML directly, however you can extend TextView and set a default font.

package com.nannu;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NanTV extends TextView{

    private Context c;
    public NanTV(Context c) {
        super(c);
        this.c = c;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }
    public NanTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);
        // TODO Auto-generated constructor stub
    }

    public NanTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }


}

And in your layout use new TextView

<com.nannu.NanTV
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 

        />

I am posting this from my prev answer https://stackoverflow.com/a/11239305/1166537

Upvotes: 14

karllindmark
karllindmark

Reputation: 6071

If you create your own TextView derivation, you could add an XML-attribute that the class handles the programmatical way. That way, you specify a certain font and it'll be set runtime, but you do it via XML. :)

Otherwise, there's no known way to achieve the requested behaviour.

Upvotes: 3

Related Questions