Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change app wide font-size using a theme for Android

Tags:

android

Is it possible to make every bit of text larger on an Android application, mimicking what happens when you change the default font size in the global settings screen? I want for the user to select a font size in the settings screen and all of the text to change, without needing to put extra style tags onto my individual screens.

I have three themes so far which correctly change the font size for TextView's but not things like EditText, or ListView elements

<style name="AppThemeSmall" parent="AppTheme">
    <item name="android:textViewStyle">@android:style/TextAppearance.Small</item>
    <item name="android:textAppearance">@android:style/TextAppearance.Small</item>
</style>

<style name="AppThemeMedium" parent="AppTheme">
    <item name="android:textViewStyle">@android:style/TextAppearance.Medium</item>
    <item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
</style>

<style name="AppThemeLarge" parent="AppTheme">
    <item name="android:textViewStyle">@android:style/TextAppearance.Large</item>
    <item name="android:textAppearance">@android:style/TextAppearance.Large</item>
</style>
like image 332
Carrie Hall Avatar asked Apr 11 '13 13:04

Carrie Hall


People also ask

How do I change the font size on a specific app?

To make your font size smaller or larger: On your device, open the Settings app. Search and select Font size. To change your preferred font size, move the slider left or right.

How do I change the font on my theme app?

If you wish to download new font styles on your Xiaomi mobile phone, follow these steps: Go to the Themes app. Now, tap on the fonts tab on the bottom right. Select the font you want to download and hit the 'Download' option.


1 Answers

I ended up adding styles to the xml, it felt cleaner that way and I had more control.

each layout xml (inc. EditText, AutoCompleteTextView)

 <TextView
   style="?textTitle"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/ref" />

attrs.xml

<attr name="textTitle" format="reference" />

styles.xml (note that I've overridden the spinner style as well to keep the sizes consistent

<style name="AppTheme.Small">
    <item name="textTitle">@style/small_title_text</item>
    <item name="android:spinnerItemStyle">@style/spinner_small</item>
</style>

<style name="AppTheme.Medium">
    <item name="textTitle">@style/medium_title_text</item>
    <item name="android:spinnerItemStyle">@style/spinner_medium</item>
</style>

<style name="AppTheme.Large">
    <item name="textTitle">@style/large_title_text</item>
    <item name="android:spinnerItemStyle">@style/spinner_large</item>
</style>


<!-- SMALL FONT SIZE -->
<style name="small_title_text">
    <item name="android:textSize">15sp</item>
    <item name="android:textStyle">bold</item>
</style>


<!-- MEDIUM FONT SIZE -->
<style name="medium_title_text">
    <item name="android:textSize">19sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="medium_list_text"></style>

<!-- LARGE FONT SIZE -->
<style name="large_title_text">
    <item name="android:textSize">23sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="large_list_text"></style>

<!-- SPINNER STYLES -->
<style name="spinner_small">
    <item name="android:textSize">17sp</item>
    <item name="android:paddingTop">0dip</item>
</style>

<style name="spinner_medium">
    <item name="android:textSize">21sp</item>
    <item name="android:paddingTop">0dip</item>
</style>

<style name="spinner_large">
    <item name="android:textSize">25sp</item>
    <item name="android:paddingTop">0dip</item>
</style>

Then I set the theme before every activity is created.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PreferenceHelper mPrefs = PreferenceHelper.getInstance(this);
    setTheme(mPrefs.getTheme());
    setContentView(R.layout.activity_list);
like image 75
Carrie Hall Avatar answered Sep 22 '22 08:09

Carrie Hall