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>
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.
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.
With the release of Android Oreo you can use the support library to reach this goal.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With