3

I am building a small Android App and want the font for the entire App and all the text no matter where to be Roboto, without me having to change it. So far the answer I could find regarding this haven't helped me.

I created an assets folder and a font folder, but I do not want to have to import it into every activity/textview over and over again.

Is there a way I can do that?

Ozymandias
  • 199
  • 6
  • 17

2 Answers2

2

The answer is yes.

Global Roboto light for TextView and Buttonclasses.

<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. At the end apply the style as the theme of the application.

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

It will works 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.

And you can use this text view class in your XML file. Have a look at this thread https://stackoverflow.com/a/16406494/1841777

Community
  • 1
  • 1
Shishram
  • 1,514
  • 11
  • 20
1

Okay, I found the answer to my question and also the reason I didn't understand the answer posted before.

1.) The file you are looking to modify is the global styles.xml, found in: main>res>values>style.xml

If you already chose a theme for your app in the design window of your main activity, the file will already contain:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- Customize your theme here. -->

</resources>

As the comment says, there you can customize your app.

Add the styles you want, like tomrozb's answer suggests: https://stackoverflow.com/a/16407123/3910425

then add the theme to the AndroidManifest.xml, if it isn't already there.

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

The part AppTheme corresponds to the style name in the styles.xml.

My biggest problem was that I didn't know which files to add what to. So now I get it and I hope maybe somebody else will find this helpful, too :)

Community
  • 1
  • 1
Ozymandias
  • 199
  • 6
  • 17