4

How to use Open Sans font style for textview in android? by default in font-family Open Sans is not available.

Faisal
  • 139
  • 1
  • 2
  • 9

5 Answers5

5

Android O and Android Support Library 26 add support for Downloadable Fonts.

Google Fonts is shipping a Font Provider in Google Play Services.

Please read official doc: https://developers.google.com/fonts/docs/android

3

use this lib for font change on hole app Calligraphy

use this code for change perticuler text font.

put the font file in your assets folder. In my case I created a subdirectory called fonts.

    TextView tv = (TextView) findViewById(R.id.textViewName);
    Typeface face = Typeface.createFromAsset(getAssets(),"fonts/opansans.ttf");
    tv.setTypeface(face);
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
1

enter image description here

Step 1 : create an assest folder -> fonts folder -> place your .ttf file.

Step 2 : Create your custom textview class like below :

public class LotaRegularTextView extends TextView {

    public LotaRegularTextView(Context context) {
        super(context);
        this.setTypeface(Typeface.SERIF);
    }
    public LotaRegularTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(Typeface.SERIF);
    }
    public LotaRegularTextView(Context context, AttributeSet attrs, int 
           defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setTypeface(Typeface.SERIF);
    }
}

Step 3 : Add FontsOverride.class to your package this class replace default font family to your font family

  public final class FontsOverride {

    public static void setDefaultFont(Context context,
            String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
            final Typeface newTypeface) {
        try {
            final Field staticField = Typeface.class
                    .getDeclaredField(staticTypefaceFieldName);
            staticField.setAccessible(true);
            staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
  }

Step 4 : Write line in application class on create method. that replace "serif" font to your font family

FontsOverride.setDefaultFont(this, "SERIF", "fonts/Lato-Regular.ttf");

Step 5 : How can use custom textview class like below

 <com.example.widget.LotaRegularTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/padding10"
        android:text="Ashish"
        android:textColor="@color/gini_gray_color_7d7d7d"
        android:textSize="@dimen/s_common_a"/>
ashish
  • 848
  • 6
  • 16
0

You can check that answer https://stackoverflow.com/a/3651168/6792992 You can use any kind of font in the same way as the answer suggesests

Community
  • 1
  • 1
0

You must add a custom font. First download the font archive .ttf which in your case can be found at : https://www.fontsquirrel.com/fonts/open-sans once you have it I recommend you this tutorial to add custom fonts: https://futurestud.io/tutorials/custom-fonts-on-android-using-font-styles

Shohn
  • 967
  • 11
  • 13