binW
binW

Reputation: 13712

Android: How to change app font size from code

I have a requirement where I need to change the application font size based on user provided values. How can I do that? I have googled but it seems that this can only be done is from XML layouts.

Upvotes: 5

Views: 14318

Answers (6)

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6758

Define a style in styles.xml file under the values directory and customize whatever you want. I'm using textSize 11sp

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/textColorTitle</item>
        <item name="android:textSize">11sp</item>
        <item name="android:textColorSecondary">@color/colorAccent</item>
        <item name="buttonStyle">@style/ButtonStyle</item>
    </style>

In the menifest file apply to your activities like

<activity
            android:name=".view.activities.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme" />

Upvotes: 0

MoJa
MoJa

Reputation: 7

You must create file styles.xml in values and in this file:

<resources>
    <style>
        <item name="android:textSize">20sp</item>
    </style>
</resources>

Upvotes: 0

binW
binW

Reputation: 13712

It seems like I cant change app font size and I have to change font size for individual element seperateley.

Upvotes: 0

Aleadam
Aleadam

Reputation: 40401

The only way I can think of is to extend TextView and make them check a global textSize variable on every onDraw(). It should not add too much overhead to the drawing.

Upvotes: 1

user485498
user485498

Reputation:

You can alter text size for textviews with

textview.setTextSize(int_value);

Is this what you meant?

Upvotes: 5

CommonsWare
CommonsWare

Reputation: 1007624

Call setTextSize() on each TextView (or other widgets that inherit from TextView, such as Button).

Upvotes: 3

Related Questions