How to use Open Sans
font style for textview in android? by default in font-family Open Sans
is not available.
5 Answers
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

- 63
- 1
- 4
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);

- 4,198
- 2
- 22
- 29
-
Editors fixed the grammar issues with the comment, why were they reverted? – depau Sep 27 '19 at 18:58
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"/>

- 848
- 6
- 16
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

- 1
- 1

- 399
- 7
- 23
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

- 967
- 11
- 13