Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default font family for entire Android app

I'm using the Roboto light font in my app. To set the font I've to add the android:fontFamily="sans-serif-light" to every view. Is there any way to declare the Roboto font as default font family to entire app? I've tried like this but it didn't seem to work.

<style name="AppBaseTheme" parent="android:Theme.Light"></style>  <style name="AppTheme" parent="AppBaseTheme">     <item name="android:fontFamily">sans-serif-light</item> </style> 
like image 441
tomrozb Avatar asked May 06 '13 18:05

tomrozb


People also ask

How do I change the text style of all apps?

Change Your Font Style in Android Settings As an example, on Samsung Galaxy devices the default pathway is Settings > Display > Font and screen zoom > Font Style. Afterward, you can tap to select a font, see the immediate change, and select Apply to confirm your new selection.


2 Answers

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.

like image 69
tomrozb Avatar answered Sep 20 '22 20:09

tomrozb


With the release of Android Oreo you can use the support library to reach this goal.

  1. Check in your app build.gradle if you have the support library >= 26.0.0
  2. Add "font" folder to your resources folder and add your fonts there
  3. Reference your default font family in your app main style:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="android:fontFamily">@font/your_font</item>    <item name="fontFamily">@font/your_font</item> <!-- target android sdk versions < 26 and > 14 if theme other than AppCompat --> </style> 

Check https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html for more detailed information.

like image 45
João Magalhães Avatar answered Sep 19 '22 20:09

João Magalhães