How to Set Default Font Family For Entire Android App

How to set default font family for entire Android app

The answer is yes.

Global Roboto light for TextView and Button classes:

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

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

<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>

Just select the style you want from list themes.xml, then create your custom style based on the original one. At the end, apply the style as the theme of the application.

<application
android:theme="@style/AppTheme" >
</application>

It will work only with built-in fonts like Roboto, but that was the question. For custom fonts (loaded from assets for example) this method will not work.

EDIT 08/13/15

If you're using AppCompat themes, remember to remove android: prefix. For example:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>

Note the buttonStyle doesn't contain android: prefix, but textViewStyle must contain it.

Set default font family for Android app

Thanks KDeogharkar!

Looks like I would have to add the code inside of styles.xml in values folder. It worked :)

How to set custom font for a whole application in Android?

Write a class

public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}

now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp

<application
android:name=".MyApp"
...
>
...
</application>

So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.

Update
Put font file under assets/fonts/your_font_file.ttf

Put this line under onCreate method of your application class(MyApp)

TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");

Source File for TypefaceUtil

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

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("serif", 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 update your style.xml file

put the below line your style which is included for your activity in manifest file

  <item name="android:typeface">serif</item>

Hope this helps

how to set default font family available in android to action bar title

With a Toolbar you can use:

    <com.google.android.material.appbar.MaterialToolbar
app:titleTextAppearance="@style/myTitleTextAppearance"

with:

    <style name="myTitleTextAppearance" parent="TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.AppCompat.Subhead">
<item name="fontFamily">sans-serif-medium</item>
<item name="android:fontFamily">sans-serif-medium</item>
.....
</style>

with an ActionBar you add in your app theme the actionBarStyle attribute:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">@style/myActionBar</item>
</style>

with:

<style name="myActionBar" parent="Widget.MaterialComponents.ActionBar.PrimarySurface">
<item name="titleTextStyle">@style/myActionBarTextTitle</item>
</style>

<style name="myActionBarTextTitle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="fontFamily">sans-serif-medium</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>

How to change the default font family in Flutter

You can change the default font family of your Flutter app by following the below steps:

1. Add your font files into your project folder. Say Project Folder > assets > fonts > hind.

2. Declare the font family with font files with style in your project's pubspec.yaml file as (An example):

Sample Image


  1. In the MaterialApp widget of your main class file, define the default font family as:

Sample Image

Is it possible to change font family when a default is defined for the AppTheme?

What you did is correct. But if you're looking for an alternative, you could try applying a font programmatically in your code for the views you want like this:

//Setting typefaces - this will give stylish fonts
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/FONT_NAME.EXTENSION");
textView.setTypeface(typeface);

Now since this is in program, it should override the default font set by the theme. I'm not sure but this should work. If you try it, please let me know.

How to change default font of the android app?

There is a grate library for custom fonts in android: custom fonts

Here is a sample how to use it.

In gradle you need to put this line:

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

Then make a class that extends application and write this code:

public class App extends Application { 
@Override public void onCreate() {
super.onCreate();

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("your font path")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}

In the activity class put this method before onCreate:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

In your manifest file write like this:

<application
android:name=".App"

It will change the whole activity to your font!. I'ts simple solution and clean!



Related Topics



Leave a reply



Submit