Roberto Orgiu
Aug 14 · 3 min read

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.

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

Disclaimer: in our scenario, we had limited time and we wanted to deliver something that was as clean as possible, so we went a bit off the road.

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:

Note: Below is the extension function we made, on SeekBar, to reduce some boilerplate:

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.

Bindings

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:

Conclusion

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.

Thanks to Wojciech Dziemianczyk and Sebastiano Poggi

Roberto Orgiu

Written by

Android developer and GDE for Android

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade