A story about text size, data binding, and reacting

A lot of text based reading apps give users the ability to change text size. An ideal implementations is one which provides users the ability to modify this locally, without affecting the global system settings. There are may ways of doing so, and it usually is one of the first features that gets implemented in these sorts of products.

While being rock-solid and battle-tested, this feature might become old and hard to maintain over time. In our situation, we had a PublishSubject that would emit every time the user changes the text size from a Dialog. We’d listen to these events and react accordingly by adjusting any TextViews on screen.

Image for post
Image for post
We didn’t use a Dialog, but the idea is the same

A solution

The first step is to declare in our layout the variable, with type and name, within the XML layout. We then use the * to multiply the text size by the scale we want.

In order to be able to change the values in the layout, we want to be notified every time text size changes: posting a new value via LiveData makes it really simple, and we can do it every time the user modifies the SeekBar:

We defined the LiveData as an extension, so we could send an initial value right away:

We need to do so otherwise our text would not appear until we change its size, and this is definitely something we would want to avoid.


Creating bindings is actually straightforward, and we only need to link the variable we declared in the layout with the LiveData we defined in our code:


This approach may come in handy when you have an older architecture, maybe based on RxJava, and you want to react properly in your layouts without too much trouble.

Arguably, a solution that only involves data binding would be easier to manage and code (in a scenario like this one), but you might find yourselves in a situation where you can’t really achieve that for multiple reasons, and you have to get creative about it — for instance when your text size is managed in a Dialog and you have to react to it.

If you want to check out the complete solution (and one that only involves data binding), feel free to hit the GitHub repo.

Written by

Android developer and GDE for Android

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store