0

So I wanted to make a custom font for my buttons and textviews in my app based on this answer. I know it's doable with this much simpler way, if I just put this in the activity onCreate:

    String fontPath = "fonts/Geometos.ttf";
    Button mButton = (Button) findViewById(R.id.login_button);
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    mButton.setTypeface(tf);

but it's not a nice solution if I want to use custom fonts for many views.

I created the CustomFontHelper, FontCache and MyButton classes just like in the linked answer. I have the same attrs.xml, my style.xml looks like this:

    <style name="Button" parent="@android:style/Widget.Button">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:textSize">50sp</item>
    <item name="android:textColor">@color/SecondaryTextColor</item>
    <item name="uzoltan.readout:font">fonts/Geometos.TTF</item>
</style>

and here is the view declaration in my activity xml:

    <uzoltan.readout.Font.MyButton  //my package name is uzoltan.readout and I have the MyButton, FontCache and CustomFontHelper classes in a package called Font
    style="@style/Button"
    android:id="@+id/login_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="@string/login_button_text"/>

And of course I have the ttf file in place, since it works well with the simpler solution I copied at the start. I watched what's happening in debug and in the FontCache class this is what happening:

Typeface tf = fontCache.get(name);  //returns null
    if(tf == null) {
        try {
            tf = Typeface.createFromAsset(context.getAssets(), name); //still returns null
        }
        catch (Exception e) { //Runtime exception, font asset not found fonts/Gemetos.TTF
            return null;
        }

meaning in CustomFontHelper class the setTypeFace never gets called. So my question is what's wrong with the createFromAsset call in FontCache? Why does it return null?

EDIT: I also tried skipping FontCache and have this line in CustomFontHelper:

Typeface tf = Typeface.createFromAsset(context.getAssets(), font);

but this causes a runtime faliure with the same reason: "java.lang.RuntimeException: Font asset not found fonts/Geometos.TTF" (My directory for the font is src/main/assets/fonts/Geometos.TTF

EDIT 2: THE PROBLEM WAS .TTF (with capital letters), so if I use <item name="uzoltan.readout:font">fonts/Geometos.ttf</item> in styles.xml than everything works fine.

Community
  • 1
  • 1
logi0517
  • 813
  • 1
  • 13
  • 32

2 Answers2

1

You can use custom button class as given below.Put your font in asset/font folder.

public class CustomButton extends Button{


    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        // TODO Auto-generated constructor stub
    }
    public CustomButton(Context context) {
        super(context);
        init();
        // TODO Auto-generated constructor stub
    }
    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
        // TODO Auto-generated constructor stub
    }
    private void init(){
        Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova-Bold.ttf");
        setTypeface(font_type);
    }
}

Now you can use the button in xml as given below.

<model.CustomButton
            android:id="@+id/search"
            android:layout_width="@dimen/edittext_width_large"
            android:layout_height="@dimen/button_height"
            android:layout_below="@+id/cars"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/pad_20dp"
            android:background="@drawable/button_pressed_bg"
            android:text="@string/find_car"
            android:textColor="@color/white" />
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Jinosh P
  • 341
  • 2
  • 8
  • I'm gonna accept your answer because it gave the the idea to me that fonts/Geometos.TTF is wrong and I should use fonts/Geometos.ttf in my styles.xml, which worked :D I'm gonna comment to the original poster on that other question, maybe he will edit his anwser, that's the only reason I used TTF too for some reason. – logi0517 Oct 01 '15 at 13:18
  • or not, I need 4 more reputation so I can comment to other questions. – logi0517 Oct 01 '15 at 13:21
1

You can use Calligraphy library.

https://github.com/chrisjenx/Calligraphy

Allow to declare in xml your font and configure it quickly.

j.seisson
  • 51
  • 2