2

I am trying to add a custom roboto font to my android button; however, most solutions aren't working in this case.

I am trying to use solutions from here...Setting Button text font in android

I have fonts right here in my assets folder.

enter image description here

Here is a snippet of my code:

  Button block_block_font =(Button) findViewById(R.id.block_button);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
        block_block_font.setText("show");
        block_block_font.setTypeface(typeface);

        Button security_settings_button_font =(Button) findViewById(R.id.security_settings_button);
        security_settings_button_font.setText("show");
        security_settings_button_font.setTypeface(typeface);

        Button blacklist_whitelist_button_font  =(Button) findViewById(R.id.blacklist_whitelist_button);
        blacklist_whitelist_button_font.setText("show");
        blacklist_whitelist_button_font.setTypeface(typeface);

However, when I apply that solution for editing I produce this error:

06-14 16:59:56.677  26389-26389/com.spicycurryman.getdisciplined10.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spicycurryman.getdisciplined10.app/com.spicycurryman.getdisciplined10.app.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
            at android.app.ActivityThread.access$700(ActivityThread.java:152)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5328)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: native typeface cannot be made
            at android.graphics.Typeface.<init>(Typeface.java:307)
            at android.graphics.Typeface.createFromAsset(Typeface.java:281)
            at com.spicycurryman.getdisciplined10.app.MainActivity.onCreate(MainActivity.java:39)
            at android.app.Activity.performCreate(Activity.java:5250)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
            at android.app.ActivityThread.access$700(ActivityThread.java:152)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5328)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

Not sure why a native typeface cannot be made for me when it works for everyone else ...?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81
  • There would appear to be a problem with the specific font file you are trying to use. – CommonsWare Jun 15 '14 at 00:12
  • AFAIK creating your own folders in assets isn't supported by Android as of yet. –  Jun 15 '14 at 00:12
  • I don't know if that is the case because this is able to work with my textviews in another activity (the font that is) – Rohit Tigga Jun 15 '14 at 00:15
  • Yes, on continuing to research, I am wrong. (Though that holds true for drawables.) –  Jun 15 '14 at 00:15
  • And, in researching found this [potential duplicate](http://stackoverflow.com/questions/7531856/issue-when-using-a-custom-font-native-typeface-cannot-be-made) –  Jun 15 '14 at 00:17
  • I am trying to find out if my assets folder is misplaced...? – Rohit Tigga Jun 15 '14 at 00:19

3 Answers3

2

create an assets directory and put your customefont.ttf file inside that directory..

    Button bu = (Button) findViewById(R.id.custom_font); 
    Typeface    font=Typeface.createFromAsset(getAssets(),"yourCustomeFont.ttf"); 
    bu.setTypeface(font);

This way worked for specific button But what if u want to apply this font for the entire app u can use this way apply this lib compile it in your gradle file complie'me.anwarshahriar:calligrapher:1.0'

After that 2 steps remain.

1- instantiate calligrapher and set the font onCreate method in the main activity

  Calligrapher calligrapher = new Calligrapher(this);       
  calligrapher.setFont(this, "yourCustomFontHere.ttf", true);

And if you want to make this font accessed by the whole app so instead of step 1 do this instantiate the Calligraphy in the Class that extends the application class like that :

  CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/yourCustomFontHere.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build()
    );

2- inject it into the context, by overriding attachBaseContext method in the main Activity like that:

  @Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

This is the most elegant super fast way to do that ..

moahmed ayed
  • 636
  • 7
  • 10
0

Looking at your screenshot, it looks like your assets folder is in your src folder.

Move assets up to the project root and give that a try.

Brian DeWolff
  • 326
  • 1
  • 5
0

Put all your fonts in the assets folder directly and change to reference the asset name without folder name at the start, then try.

Or Remove the assets folder in src and put it app/main directly.

Tawanda Muzavazi
  • 405
  • 3
  • 14