wingsraam
wingsraam

Reputation: 187

How to set customize different font style in Android

I want to set different font style my ‘button’ text and ‘edit text box’ text like times New Roman, Calibri,Cambria and Georgia. How can i set different font,example for i want to change my login button text to Calibri font. I don't know how can i set or import font from MS Office or Font files. Please suggest me, Thank you..

MY XML CODE HERE

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="   User Name  " />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="   Password  " 
        android:fontFamily="Tekton Pro Ext"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="Tekton Pro Ext"
        android:text="   Login  " />
</LinearLayout>

Layout

enter image description here

Upvotes: 3

Views: 3661

Answers (3)

Abhishek Dabholkar
Abhishek Dabholkar

Reputation: 61

All the above answers are right. If you want to use a Button or TextView with a different font throughout the app, here's what you do.

public class FontsArePriceyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCostlyFont();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCostlyFont();
    }

    public MyTextView(Context context) {
        super(context);
        setCostlyFont();
    }

    private void setCostlyFont() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "filenameofyourfont.ttf");
            setTypeface(tf);
        }
    }
}

Then in your layouts, replace TextView with yourpackagename.FontsArePriceyTextView. Eg.

<com.company.projectname.widgets.FontsArePriceyTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Your awesome stylised text" />

You should be good to go.

Upvotes: 0

Milad Faridnia
Milad Faridnia

Reputation: 9477

Maybe its better to take a look at this posts which exactly answered what you are looking for:

1- How to change fontFamily of TextView in Android

2- How to change the font on the TextView?

for short you must do something like this : put the font in your assets folder in /fonts directory and then use this line of code:

Button bt = (Button) findViewById(R.id.myButton);
Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/epimodem.ttf");
bt.setTypeface(face);

Upvotes: 1

Zubair Akber
Zubair Akber

Reputation: 2828

You need to create fonts folder under assets folder in your project and put your TTF into it and in the main or in your activity you can set the fonts as

TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

Upvotes: 4

Related Questions