-1

I want to apply custom font in whole application widget(ActionBar,Navigation Drawer,Tabs,Toolbar,Toast) text without using any third party library so how can I achieve this? any way to got this.

Custom font in android application Widget(Actionbar,Drawer,etc.) Text without any third party .

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55

6 Answers6

4

Use .ttf file of your selected font. copy it into assets folder. Provide path of that font file to the TypeFace like this.

TypeFace mTypeFace = Typeface.createFromAsset(getAssets(), "expressway.ttf");
TextView txt = (TextView) findViewById(R.id.text);
txt.setTypeface(mTypeFace); 
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
urvi joshi
  • 158
  • 13
2

Till now,
There is no way to apply global font to all the TextView throughout whole App using Android APIs, though Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources.

Refer this link to Learn more

If we talk about libraries, there is most famous that can do the trick for you. Check this out - Calligraphy. Github page have better explanation about how you use and how things works behind.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • ok thank you, i also tried Calligraphy Library but in my first screen font not applied. – Vidhi Dave Jun 01 '17 at 08:54
  • Does it apply in other screens? Library do have function to set default path for `default font` too. – Paresh P. Jun 01 '17 at 08:56
  • If it works in rest of screens, should have work in first screen too. Anyways Dig out if you find something, otherwise apply font for that screen manually. Something is better than nothing. ;-) – Paresh P. Jun 01 '17 at 09:01
1

you can use Attribute set for this: MyTextView.class

public class MyTextView extends TextView {

                public MyTextView(Context context) {
                    super(context);
                    init(context, null);
                }

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

                public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
                    super(context, attrs, defStyleAttr);
                    init(context, attrs);
                }

                private void init(Context c, AttributeSet arts){
                    try {
                        if (!isInEditMode()) {
                            if (arts != null) {

                                TypedArray array = getContext().obtainStyledAttributes(arts, R.styleable.MyViewStyle);
                                String fontName = array.getString(R.styleable.MyViewStyle_font_name);

                                if (fontName != null) {

                                    Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
                                    setTypeface(typeface);
                                }

                                array.recycle();

                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();                           
                    }
                }
            }

attr.xml

<declare-styleable name="MyViewStyle">
    <attr name="font_name" format="string" />
</declare-styleable>

use this text view in your xml:

      <com.packg.widgets.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/description_label"           
        app:font_name="your font name" />

Android also has support for fonts check this link: fonts

vivek mahajan
  • 521
  • 3
  • 16
  • As I clearly mention i want to apply fonts to whole application for all widgets like ActionBar,Navigation Drawer,Tabs,Toolbar,Toast. According to your answer how can i apply this to every widget. – Vidhi Dave Jun 01 '17 at 07:08
  • add custom view to your action bar & navigation drawer, using this text view. – vivek mahajan Jun 01 '17 at 07:12
1

FontUtils.java

public class FontUtils {
    private static Map<String, Typeface> TYPEFACE = new HashMap<String, Typeface>();
    public static SpannableString actionBar(Activity mActivity, String title) {
        SpannableString s = new SpannableString(title);
        s.setSpan(new TypefaceSpan(mActivity, "simple.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return s;
    }
    public static void overrideFonts(final Activity mActivity) {
        mActivity.getLayoutInflater().setFactory(new LayoutInflater.Factory() {
            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                View v = FontUtils.tryInflate(name, context, attrs);
                if (v instanceof TextView) {
                    FontUtils.setTypeFace((TextView) v, mActivity);
                }
                return v;
            }
        });
    }
    public static Typeface getFonts(Context context, String name) {
        Typeface typeface = TYPEFACE.get(name);
        if (typeface == null) {
            typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+ name);
            TYPEFACE.put(name, typeface);
        }
        return typeface;
    }
    public static View tryInflate(String name, Context context, AttributeSet attrs) {
        LayoutInflater li = LayoutInflater.from(context);
        View v = null;
        try {
            v = li.createView(name, null, attrs);
        } catch (Exception e) {
            try {
                v = li.createView("android.widget." + name, null, attrs);
            } catch (Exception e1) {
            }
        }
        return v;
    }
    public static void setTypeFace(TextView tv, Context mContext) {
        tv.setTypeface(FontUtils.getFonts(mContext, "simple.ttf"));
    }
}

TypefaceSpan.java

public class TypefaceSpan extends MetricAffectingSpan {
    private static LruCache<String, Typeface> sTypefaceCache =
           new LruCache<String, Typeface>(12);

    private Typeface mTypeface;
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

inside activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        FontUtils.overrideFonts(YourActivity.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        //toolbar
        getSupportActionBar().setTitle(FontUtils.actionBar(this, getResources().getString(R.string.app_name)));         
    }
Zezariya Nilesh
  • 390
  • 2
  • 14
  • In this fonts are not applying in drawer widget. can you edit answer for drawer widget. or any suggestion for that. – Vidhi Dave Dec 20 '17 at 05:22
  • it works for me i had use it in my project so i suggest you – Zezariya Nilesh Dec 20 '17 at 05:26
  • Means for applying the fonts do i need to write any line for this? as right not it is not showing in whole application. I have written line for action bar so it is applied only on action bar – Vidhi Dave Dec 20 '17 at 05:27
1

Create font resource XML :

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

Adding fonts to a TextView :

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>
0

Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources.

  • You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio.
  • to access a font resource, we can use @font/myfont, or R.font.myfont.

Create font resource XML :

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

Adding fonts to a TextView :

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>

Using fonts programmatically :

Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

enter image description here

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55