0

I'm developing a notepad application I want to use my custom font and I'm following this code.

Typeface barType = Typeface.createFromAsset(getApplicationContext().getAssets(),"font/CaviarDreams.ttf");
    subject.setTypeface(barType);

But, I don't want to use this code because I must write this code for every textview or edittext.
Is there any way to make this easily?

Sachin
  • 3,424
  • 3
  • 21
  • 42
ihsanbal
  • 229
  • 3
  • 9

4 Answers4

0

You have to sublcass TextView/EditText and use your subclass inside the xml

private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/CaviarDreams.ttf");
            setTypeface(tf);
        }
    }

and call the init() method from the constructor your Custom TextView/EditText

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Same thing with the doing programmatically!I think maybe I can find easy way to do this in manifest or MainActivity? – ihsanbal Dec 24 '13 at 09:49
  • no there is no way to do this thing through xml. Nevertheless you can access all the view in the current hierarchy and apply the typeface. I can show you it if you want but the better solution is to subclass – Blackbelt Dec 24 '13 at 09:50
  • OK,if there is no way I will create subclass. – ihsanbal Dec 24 '13 at 09:52
  • 1
    why are you scared to subclass those classes? – Blackbelt Dec 24 '13 at 09:53
  • No,I'm not scared.I just find to best way to do this and I asked you.Thanks for all answers. – ihsanbal Dec 24 '13 at 09:55
0

You need to subclass EditText and in it's constructor set typeface

For ex:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

public class EditTextWithFont extends EditText {

    public EditTextWithFont(Context c) {
        super(c);
        init();
    }

    public EditTextWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public EditTextWithFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface barType = Typeface.createFromAsset(getApplicationContext().getAssets(),"font/CaviarDreams.ttf");
            setTypeface(barType);
        }
    }
}

You can create similar classes for textview and button

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

Global Roboto light for TextView and Button classes.

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>

<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

Just select the style you want from list themes.xml then create your custom style based on the original. At the end apply the style as the theme of the application.

<application
    android:theme="@style/AppTheme" >
</application>
Pradip
  • 3,189
  • 3
  • 22
  • 27
Muthuraja
  • 551
  • 5
  • 13
0
public static void setFont(final Context context, final View v) {
        try {
            if (v instanceof ViewGroup) {
                ViewGroup vg = (ViewGroup) v;
                for (int i = 0; i < vg.getChildCount(); i++) {
                    View child = vg.getChildAt(i);
                    overrideFonts(context, child);
                }
            } else if (v instanceof TextView ) {
                ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/helvetica.ttf"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Use this method in utility class and pass parent view as an argument, it will update all textviews with your font

Eg.

    View parent = (View)findViewById(R.id.parent);
    Util.setFont(this,parent);

Please check this Set font for all textViews in activity?

Community
  • 1
  • 1
Deniz
  • 12,332
  • 10
  • 44
  • 62