Android downloadable fonts using semi bold style

7.5k Views Asked by At

I am using google downloadable fonts as given in below link

https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html

I want to use Montserrat font style semi bold but that option is not there in android

Also in below link there is style semibold any idea how to do it

https://fonts.google.com/specimen/Montserrat

following is my textview xml after adding font family

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:fontFamily="@font/montserrat"
        android:text="@string/address"
        android:textAllCaps="false"
        android:textColor="@color/title_light_color"
        android:textSize="10sp"
        android:textStyle="bold" />
4

There are 4 best solutions below

0
Ramesh sambu On

You can download here all types of fonts montserrat

https://github.com/google/fonts/tree/master/ofl/montserrat

https://github.com/google/fonts/blob/master/ofl/montserrat/Montserrat-SemiBold.ttf

Add in your res/font folder use this

android:fontFamily="@font/Montserrat-SemiBold"

Hope it helps

0
yash786 On

Create a class named FontCache:

import android.content.Context;
import android.graphics.Typeface;
import java.util.Hashtable;

public class FontCache {
    private static Hashtable<String, Typeface> fontCache = new Hashtable<>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);

        if (tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            } catch(Exception e) {
                return null;
            }

            fontCache.put(name, tf);
        }

        return tf;
    }
}

Then put the below lines of code in your activity:

Typeface montserratFontType = FontCache.get("fonts/montserrat.ttf", MainActivity.this);
yourTextView.setTypeface(montserratFontType);

At last, create an assets folder inside your main folder then create a fonts folder inside the assets folder and put your montserrat.ttf file.

0
Poovarasan Selvaraj On

Try this , Create Customize font style for android app,

first create assets folder then create inside that font then put your downloaded font(ex: Light.ttf) enter link description here

Then add this line in activity

Typeface mycustomface1 = Typeface.createFromAsset(context.getAssets(), "fonts/Light.ttf");

then declare that textview

textview.setTypeface(mycustomface1);
0
Vukašin Manojlović On

You can create a new font in res/font:

<?xml version="1.0" encoding="utf-8"?>
<font-family
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:fontProviderAuthority="com.google.android.gms.fonts"
    app:fontProviderPackage="com.google.android.gms"
    app:fontProviderQuery="name=Montserrat&amp;weight=600"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

You can specify any supported weight by changing weight inside app:fontProviderQuery (600 for semi-bold). Android Studio will raise a FontValidationWarning, but it will work nonetheless.

Source: Google Fonts API for Android documentation