1
public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "font/Montserrat-Bold.ttf");
        TextView txt = (TextView) rootView.findViewById(R.id.tv);
        txt.setTypeface(font);

        return rootView;
    }
}

I have this on my fragment initializing a Typeface, but i get this error on my Log.

05-20 19:54:25.464 15739-15739/com.braudy.android.mesasixprofiler E/AndroidRuntime: FATAL EXCEPTION: main Process: com.braudy.android.mesasixprofiler, PID: 15739
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.braudy.android.mesasixprofiler/com.braudy.android.mesasixprofiler.MainActivity}: java.lang.RuntimeException: Font asset not found font/Montserrat-Bold.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Font asset not found font/Montserrat-Bold.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.braudy.android.mesasixprofiler.HomeFragment.onCreateView(HomeFragment.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I'm getting a "Font asset not found font/Montserrat-Bold.ttf" error.

I also setup the files in their correct folders. And tried rebuild and clean project nothing works.enter image description here

Mike
  • 4,550
  • 4
  • 33
  • 47
Braudy
  • 181
  • 1
  • 11
  • ok see [Load a simple text file in Android Studio](http://stackoverflow.com/questions/16821182/load-a-simple-text-file-in-android-studio) post accepted answer probably help – ρяσѕρєя K May 20 '16 at 12:18

4 Answers4

3

change your folder name to fonts instead of font and change it in java file also.

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Montserrat-Bold.ttf");
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • Okay, i'll try this and will notify you – Braudy May 20 '16 at 12:10
  • same Error .. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.braudy.android.mesasixprofiler/com.braudy.android.mesasixprofiler.MainActivity}: java.lang.RuntimeException: Font asset not found fonts/Montserrat-Bold.ttf – Braudy May 20 '16 at 12:12
  • this is what i get from changing font to fonts – Braudy May 20 '16 at 12:12
  • please try with clean and rebuild project – Ravi May 20 '16 at 12:13
  • i get the same errors :( i tried clean and rebuild twice.. and even close android studio then clean and rebuild. None of it worked – Braudy May 20 '16 at 12:17
  • you can try renaming the font without hyphen, all in lower case. I had a similar problem which worked fine after renaming font name. Not sure why this happens in Android studio. – Ganga May 20 '16 at 12:18
  • the weird thing is, it works in Activity even with "-" , it doesn't work in Fragments. – Braudy May 20 '16 at 12:27
  • yes there is no issue if you use `-` , it will work with that also – Ravi May 20 '16 at 12:31
1
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");
    TextView txt = (TextView) rootView.findViewById(R.id.tv);
    txt.setTypeface(font);

    return rootView;
}

and the directory

enter image description here

Braudy
  • 181
  • 1
  • 11
1
Typeface myFont = ResourcesCompat.getFont(cntext, R.font.font_name);
myTextView.setTypeface(myFont);
Sophie M
  • 351
  • 5
  • 14
0

Change Montserrat-Bold.ttf to montserrat-bold.ttf

Nahuels
  • 31
  • 1
  • 7