Set Font for All Textviews in Activity

Set font for all textViews in activity?

Solution1:: Just call these method by passing parent view as argument.

private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}

Solution2:: you can subclass the TextView class with your custom font and use it instead of textview.

public class MyTextView extends TextView {

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

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

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

private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}

}

Change the font of entire app all textview, including buttons and edittexts

Put this in your style.xml

<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>

also you have to change this.

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>

Developer site has provided information on this.
Let me know the progress

Setting custom font to all TextViews

If you need to set one font for all TextViews in android application you can use this solution. It will override ALL TextView's typefaces, includes action bar and other standard components, but EditText's password font won't be overriden.

MyApp.java

    public class MyApp extends Application {

@Override
public void onCreate()
{
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf");
}
}

TypefaceUtil.java

public class TypefaceUtil {

/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("TypefaceUtil","Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}

themes.xml

     <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>

Update for Android 5.0 or greater

As i have investigated and tested on device running api 5.0 or greater this solution is working fine because i am using the single style.xml file and not making other style.xml in values-21 folder

Although
Some users asking me that this solution not working with android 5.0 device (they may be using values-21 style.xml i guess)

So that is because Under API 21, most of the text styles include fontFamily setting, like

<item name="fontFamily">@string/font_family_body_1_material</item>

Which applys the default Roboto Regular font:

<string name="font_family_body_1_material">sans-serif</string>

So the best solution is

If Any one having problem not working for 5.0+. Don't override typeface in your values-v21 styles.

Just override font in values/style.xml and you will good to go :)

setting custom font to all my textviews

Activity is Context, so you can use "this" for the first parameter. findViewById(android.R.id.content) will be the second one.

Change the font of all the textviews in my application?

The easiest way is to extend the TextView widget:

public class FontTextView extends TextView {

private String mTypefacePath;

public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttrs(context, attrs, defStyle);
init(context);
}

public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttrs(context, attrs, 0);
init(context);
}

public FontTextView(Context context) {
super(context);
init(context);
}

private void setAttrs(Context context, AttributeSet attrs, int defStyle) {
if (isInEditMode())
return;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
mTypefacePath = a.getString(R.styleable.FontTextView_typeface);
a.recycle();
}

private void init(Context context) {
if (!TextUtils.isEmpty(mTypefacePath)) {
try {
setTypeface(Typeface.createFromAsset(context.getAssets(),
mTypefacePath));
} catch (Exception ex) {
// could not create the typeface from path
}
}
}}

You also need to define your typeface attribute.
Take a look on this to see a useful explanation about.

How to set fontFamily for all text in the app?

How to set fontFamily for all text in the app?

Solution

If you want use just android-default-font (sans, serif, mono).

Try this.

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-thin</item>
</style>

Additional

If you want to specify a custom font as the default font, you can use the Calligraphy library. This library is provides various features related to font.

How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

Sample Image

in combination with

android:textStyle="normal|bold|italic"

this 16 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Black
  • Roboto-Black italic
  • Roboto-Medium
  • Roboto-Medium italic

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>


Related Topics



Leave a reply



Submit