Hari Krishnan
Hari Krishnan

Reputation: 6302

How to use different set of text sizes in android?

In my app, i need to keep an action to change the font size. I am trying to keep three set of values for text sizes (small,medium and large). How should i switch between them? My idea is to use one set of font resource at a time.

For more clarification:

In Theme.AppCompat.DayNight.DarkActionBar, to shift between night mode on and off in i can call

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

and

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

Here, each will use different set of resource values for night mode on and off. I am looking for somewhat such a method to shift between different font sizes. Any help is appreciated.

Upvotes: 2

Views: 151

Answers (1)

Marcin Koziński
Marcin Koziński

Reputation: 11044

There's something like that built into the operating system. If you go to Settings > Accessibility you'll find a Large text toggle or a slider to make text bigger (depending on phone). This will work in your app as long as you use sp for your font sizes (for example android:textSize="16sp").

If you're going to use that, you'll have to remember that the setting is in the phone settings, I don't think there's a way for you to change it from your app. And it applies to all the apps, not just to yours, so everything in the system would get bigger. Which might be not what you require.

Otherwise you can try to do this manually in your app. One way would be to create three themes that have your different font sizes. Then you would set an appropriate theme on the context in your Activity.onCreate() based the setting you saved somewhere (in SharedPreference for example).

But I don't think you can just keep different font sizes in different resource subdirectories. The list of resource qualifiers is predefined and I don't think there's something for switching font sizes in there.

Upvotes: 1

Related Questions