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 :)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With