5

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" />
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
  • what is the font name you are sending as a query parameter of *FontRequest* ? Is it "Montserrat-SemiBold.ttf" refer: https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts#programmatically – ashishdhiman2007 Jan 03 '19 at 12:24

4 Answers4

2

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

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
1

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

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
0

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.

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
yash786
  • 1,151
  • 8
  • 18
0

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);