Reading Time: 5 minutes

Providing the user the possibility to choose the font sizes used in the app is a nice accessibility feature, appreciated by many users. Though, it was pretty complicated to implement this in the view world. I recently had to implement this in a Compose app and I initially had no idea how to implement it and I couldn’t find any examples or tutorials. Luckily, thanks to the flexibility of Compose I was able to find a very easy solution in no time.

In this article, I will try to show you how to implement something like this:

As usual, I prepared a simplied (and less nice looking) sample and if you want to directly jump to the code, here is the link to the github repo.

***

As you know, we are using a Typography class in our Compose themes to define all the fonts we use in our application. You might even have a custom one, it doesn’t matter. The key to easily implement this feature in your app and to make it work everywhere, is to apply the font size changes right there where they were defined: in the theme. In our case, we wanted to decrease or increase all font sizes by plus/minus 2 pixels and provide 4 alternative font size sets. This can be classified with an enum:

Copy to Clipboard

fontSizeExtra is the difference that will be applied in font sizes with respect to the default size. Icons are the icons we show in the selection menu and keys are used for saving user’s choice in preferences. Of course, you can adapt those to your needs. Then, instead of the Typography object defined in the theme as a val, we convert it to a function, with the fontSizePrefs as an argument:

Copy to Clipboard

Here we are simply applying the font size change requested by the user to all fonts defined in the theme. Plus 2, minus 2, simple maths.. Notice that changes are also applied to line heights. Because if you increase the font size without increasing the line height, your texts won’t look nice. In our case we use a line height multiplier, it could be also a simple addition. (I provide here a subset for the sake of simplicity, actually we need to adapt all fonts.)

Then in the app theme, font size preferences are passed as a parameter and typography is got based on font size preferences:

Copy to Clipboard

Finally, we pass the font size preferences of the user to the main theme at root level:

Copy to Clipboard

Selected font sizes will be applied in all the view hierarchy and can be dynamically changed without relaunching the page. As easy as that!

If there is a part where you don’t want the sizes to be dynamically changed, you can override the theme there by simply wrapping that part in DynamicFontSizesTheme {} with default font size preferences. Little remark at this point: if you have the bad habbit of wrapping all your composables with your theme again and again below the hierarchy, it can override the preferences passed from root and then you might wonder why it is not working in some parts of the screen.

Now, let’s look at the implementation of the font selection menu. In our case this is an item in the top menu, so it can simply be added to the top bar with a DropdownMenu:

Copy to Clipboard

.. where FontSelectionMenu is as follows:

Copy to Clipboard

Notice that I have wrapped selection menu and the top bar with the theme with default font size preferences, because I didn’t want the font size changes to be applied there.

Of course, we should also save and persist user’s choices. You can save this in preferences or in data store. I won’t go over those details. You can see the complete sample on Github. And that’s all! Overall, I think it was surprisingly easy to implement this with Compose and we are pretty proud to provide this nice feature to our users.

Thanks for reading and happy coding!

Written by Oya Canli

Software Engineer

After trying out different things, Oya found out that her heart beats for the green robot. She loves challenges even the weird bugs. She's always on the lookout for the newest techniques in Android development.

Published on: November 28th, 2023

Newsletter

Become an app expert? Leave your e-mail.

nederlandse loterij en egeniq
pathe thuis en egeniq
rpo and egeniq
mvw en egeniq
rtl and egeniq