Ayham Najem
Ayham Najem

Reputation: 75

Button change font family in android studio

I want to change the font family of the button. I tried to change it trying this code:

Button txt = (Button)findViewById(R.id.button1);
Typeface font = Typeface.createFromAsset(getAssets(), "sfont.ttf");
txt.setTypeface(font);

but I am getting an error, and if I ignored it the app crashes every time.

This is the Layout XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>

<TextView

    android:text="@string/enter"
    android:textColor="#000"
    android:textSize="16dp"
    android:textAlignment="center"
    android:id="@+id/**button1**"text1
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="20dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/**text1**" button1
    android:padding="30dp"
    android:layout_gravity="center"
    android:text="@string/entenbut"
    />

PROBLEM SOLVED There was a problem in the XML code Thanks everyone :)

Upvotes: 5

Views: 9389

Answers (2)

Hamid
Hamid

Reputation: 1563

In android studio there is not directory "src" but there is "res". to create asset directory,first right click on the "app" then: new -> Folder -> Assets Folder. after this,copy fonts to this directory. click here for more info

Upvotes: 1

Silo&#233; Bezerra Bispo
Silo&#233; Bezerra Bispo

Reputation: 2244

Make a folder named "Assets" in your folder "src" (if you use Android Studio) and use:

Typeface tfFutura = Typeface.createFromAsset(getAssets(), "sfont.ttf");

An observation: look if in your file are "TTF" or "ttf". I had problems with it.

Upvotes: 4

Related Questions