androidandroid-assetsandroid-fonts

android studio cannot find custom font file


I am using a simple TextView in my activity and want to make it appear in a custom font 'Black Chancery'. I have put the blkchcry.ttf file at location app>src>main>assets>fonts>blkchcry.ttf. But, the android studio can't find that font. Here's my AboutusActivity.java code

public class AboutusActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aboutus);

    TextView tx = (TextView)findViewById(R.id.title);
    Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/blkchcry.ttf");
    tx.setTypeface(custom_font);
    }
}

and following is the activity_aboutus.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AboutusActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="About Rathi Classes"
        android:textSize="20sp"
        android:layout_margin="10dp"
        android:gravity="center_horizontal"/>

    <TextView
        android:layout_below="@+id/title"
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/about_text"
        android:textSize="20sp"
        android:layout_margin="10dp" />

</RelativeLayout>

and here is the logcat

Process: net.softglobe.rathiclasses, PID: 8894
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.softglobe.rathiclasses/net.softglobe.rathiclasses.AboutusActivity}: java.lang.RuntimeException: Font asset not found fonts/blkchcry.ttf
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.RuntimeException: Font asset not found fonts/blkchcry.ttf
    at android.graphics.Typeface.createFromAsset(Typeface.java:206)
    at net.softglobe.rathiclasses.AboutusActivity.onCreate(AboutusActivity.java:17)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

I have tried cleaning the project but it failed to resolve the problem. Please help.


Solution

  • Paste Font here like this and also make your font name in small case :-

    app  > src > main > assets > fonts > blkchry.tff
    

    set Font in java File like this :-

    public class AboutusActivity extends Activity {
     Context mContext;
     Typeface fontblkchry;
    
     @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_about_us);
    
          fontblkchry = Typeface.createFromAsset(mContext.getAssets(), "fonts/blkchry.ttf");
          textview1.setTypeface(fontblkchry);
        }
    }