Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 509
Ayham Najem Avatar asked May 05 '15 13:05

Ayham Najem


Video Answer


2 Answers

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.

like image 71
Siloé Bezerra Bispo Avatar answered Sep 29 '22 20:09

Siloé Bezerra Bispo


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

like image 45
Hamid Avatar answered Sep 29 '22 19:09

Hamid