Carrie Hall
Carrie Hall

Reputation: 931

Change app wide font-size using a theme for 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>

Upvotes: 6

Views: 12632

Answers (2)

Carrie Hall
Carrie Hall

Reputation: 931

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

Upvotes: 7

Kaskraker
Kaskraker

Reputation: 36

Create your own custom widget which extends from a basic widget. During initialisation retrieve the font-setting from the shared preferences. I guess you will store it there, when settings are changed. During initialisation the textsize of the widget is set to the stored setting. In code:

public class MyCustomTextView extends TextView {

    public MyCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }   

    public MyCustomTextView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {

        float size = .... // retrieve from your shared preferences
        setTextSize(size);
    }
}

This way you can extend all widgets which have a textsize attribute and change it during initialisation.

Upvotes: 0

Related Questions