0

I have font file roboto in my asset folder. I want to change the normal font to roboto at once . If i use typeface like below:

 TextView forgotPwd = (TextView) findViewById(R.id.tv_forgot_pwd_text);

     Typeface type = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");
            forgotPwd.setTypeface(type);

Then i have to check and give the same code for every text view manually.But my need is if i set Typeface or any thing it should reflect in the whole application's text or text view.

Now i used

 public class MyApp extends Application {
              @Override
              public void onCreate() {
                 super.onCreate();
                TypefaceUtil.overrideFont(getApplicationContext(), "Roboto", "fonts/Roboto-Regular.ttf");
              }
              }

I used application class and also took the help of some custom class but its not working .

public class TypefaceUtil {

public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {

    final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Map<String, Typeface> newMap = new HashMap<String, Typeface>();
        newMap.put("Roboto", customFontTypeface);
        try {
            final Field staticField = Typeface.class
                    .getDeclaredField("sSystemFontMap");
            staticField.setAccessible(true);
            staticField.set(null, newMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } else {
        try {
            final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
            defaultFontTypefaceField.setAccessible(true);
            defaultFontTypefaceField.set(null, customFontTypeface);
        } catch (Exception e) {
            Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
        }
    }
}

}

now style.xml is

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlNormal">#FFFFFF</item>
        <item name="colorControlActivated">#FFFFFF</item>
        <item name="android:textColorHint">@color/black</item>
        <item name="android:typeface">Roboto</item>
    </style>

And in the manifest:

    android:name="pkgname.MyApp"

But still i am not getting the reflection in my application please let me know that where i am missing. And guide me

Gyan S Awasthi
  • 237
  • 6
  • 14

3 Answers3

1

If you want to do so ,then you should just extend View class (TextView) and add that specific font family that you want, you need to make CustomTextView class and change here also you can add some other attribute in attr folder.

1

You can Calligraphy.

It's pretty easy to use.

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("Roboto-Regular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()

Create a BaseActivity or add this to all your activities, your call

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
elmorabea
  • 3,243
  • 1
  • 14
  • 20
0

Just try changing the Font name to Default.

previous:-
TypefaceUtil.overrideFont(getApplicationContext(), "Roboto", "fonts/Roboto-Regular.ttf");

change it to TypefaceUtil.overrideFont(getApplicationContext(), "Default", "fonts/Roboto-Regular.ttf");

Better you follow this link Is it possible to set a custom font for entire of application?