MaximumGoat
MaximumGoat

Reputation: 3307

Can I globally increase the text size for all my UI widgets at run-time?

I would like to implement adjustable text size in my application for the sake of readability - quite a few of my users have requested it. The idea is that the user will indicate a preference for larger or smaller text, I'll store it in a SharedPreference, and then I'll make use of that value whenever I load a new activity.

Obviously, I could load up every view that displays text whenever I initialize an activity's layout, and then change the text size on all of them by name, but that seems verbose, error-prone, and difficult to maintain. I would prefer instead to increase/decrease size based on existing size, or even better, existing style (so that if I cared to, 12 could become 16, and 20 could become 30).

I think in my ideal world, I would be able to load all my XML-defined styles before calling setContentView, alter them, "save" them, and have those changes take effect on the layouts I load from that point forward. Having read a bit more, that seems impossible: "The Android SDK tools compile your application's resources into the application binary at build time" and then there's nothing in the Resources class or the AssetManager class underneath that suggests I can change these styles at run-time.

That seems to leave me with traversing my layout in each onCreate, finding the text size of every view for which that value is relevant, and setting a text size that is, say 2 sp bigger or smaller (figuring out sp based on screen density).

Anyone have any thoughts on this? Maybe some of you have been through this before?

Upvotes: 1

Views: 2078

Answers (1)

havexz
havexz

Reputation: 9590

Try creating a theme.

http://developer.android.com/guide/topics/ui/themes.html

Themes can be applied to the whole activity. You can apply theme using setTheme of Activity.

http://developer.android.com/reference/android/content/ContextWrapper.html#setTheme(int)

Upvotes: 2

Related Questions