Sep 24 | 7:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Sep 24 | 7:30 PM |
Tad F. | has entered the room |
Mark M. |
hello, Tad!
|
Tad F. |
hi Mark!
|
Mark M. |
how can I help you today?
|
Tad F. |
I have a question about best practice for adjusting font sizes in the UI at runtime.
|
Tad F. |
View paste
|
Mark M. |
I take it that simply using sp units, and relying on the user to have font scale set up in Settings, isn't an option?
|
Tad F. |
Well, I considered that, but I was a bit concerned that this would be too confusing for that user.
|
Tad F. |
These are typically elderly, or those with some dementia
|
Tad F. |
And their device is a dedicated "digital frame" type (table)
|
Tad F. |
Tablet
|
Mark M. |
well, a table is just a tablet with legs :-)
|
Tad F. |
But I can't guarantee that the tablet would ONLY be used for that
|
Tad F. |
So I can't "take over the entire tablet"
|
Mark M. |
sure, but they probably need the bigger fonts in the other stuff that they are using on the tablet
|
Tad F. |
"probably" being the key word.
|
Mark M. |
but, regardless, if you're not going that route, that's fine -- you know your audience better than I do
|
Tad F. |
Let's just explore that a bit though
|
Sep 24 | 7:35 PM |
Tad F. |
So when you say "use SP", you mean in all the layouts use SP rather than DP?
|
Mark M. |
for text size, yes
|
Mark M. |
you might need it elsewhere as well, depending on how you size your widgets
|
Tad F. |
The other issue for me (and probably the most important) is that not ALL the screens need to have this adjustment. This is because some of them wouldn't normally be accessed by this audience, but I also don't want to "turn off" that access either.
|
Mark M. |
if you're mostly a wrap_content or 0dp-and-ConstraintLayout sort of person, then text size would be the big one
|
Tad F. |
And large fonts on those screens simply won't work
|
Tad F. |
(i.e. screens that do designer type layout for image editing, for example)
|
Tad F. |
So I have maybe 8 - 10 screens that have typical TextView, EditText, RecyclerView, Spinners, etc.
|
Tad F. |
Those are what I need to adjust
|
Tad F. |
It's what that audience will spend 90% of their tiem
|
Tad F. |
time
|
Mark M. |
well, you need to bear in mind that whether *you* get the user to set up font scaling for their device is independent of whether *anyone else* got the user to set up font scaling
|
Tad F. |
Of course.
|
Tad F. |
That's one of the things that I'm concerned with.
|
Tad F. |
for my internal settings, I have an initial onboarding wizard that asks questions, and sets things up accordingly.
|
Mark M. |
that means for screens where you absolutely cannot support font scaling, you're stuck with dp... even if the specific user for that specific screen could benefit from large fonts (designer with poor eyesight, for example)
|
Tad F. |
I don't want to pop out tothe Android Settings screens, unless it's absolutely necessary
|
Tad F. |
So your suggestion in summary if I follow this route is:
|
Tad F. |
Use SP in all areas where font adjustment is acceptable, rely on Android settings to adjust it
|
Tad F. |
Other areas use DP
|
Sep 24 | 7:40 PM |
Tad F. |
Is that right?
|
Mark M. |
that's the gist of it, yes
|
Mark M. |
if you feel that is impractical for your audience, though, then you're stuck with in-app scaling
|
Tad F. |
OK - so back to my original question, within my app is there another option other than what I'm currently doing, i.e. in code adjusting the size at runtime?
|
Mark M. |
not really, though a single utility function would seem to be all you need
|
Mark M. |
have it walk the view hierarchy, and for all nodes that extend TextView, adjust the font
|
Tad F. |
Well, one to change the size, but I'd have to iterate through all the components.
|
Tad F. |
Yeah, ok right.
|
Tad F. |
Sense of a performance hit?
|
Tad F. |
I suppose do this in onCreate(), right?
|
Mark M. |
not really much different than what you would be doing now
|
Mark M. |
presumably, for activities
|
Tad F. |
or onCreateView
|
Mark M. |
presumably, for fragments :-)
|
Mark M. |
(actually, I'd probably do onViewCreated() for fragments, but either could work)
|
Mark M. |
(well, no, take that back, for this specific scenario, probably onCreateView() *is* the right answer)
|
Tad F. |
why?
|
Mark M. |
well, it gets to something that I've had here in my clipboard trying to paste into the chat when I had a second... :-)
|
Mark M. |
if the font sizes are not the same, but your adjustment is a scale factor, get the current text size, multiply by your scale factor, and set that text size on the widget
|
Sep 24 | 7:45 PM |
Mark M. |
the catch with that algorithm is that you only want to do that once
|
Tad F. |
right
|
Mark M. |
I forget the rules behind onViewCreated() and if that might get called more than once per onCreateView()
|
Mark M. |
onCreateView(), you're guaranteed that you're getting fresh widgets that need the adjustment
|
Mark M. |
I'm not 100% certain that is equally true of onViewCreated()
|
Tad F. |
Ok
|
Mark M. |
FWIW, in the future, your scenario becomes a *lot* simpler with Jetpack Compose
|
Tad F. |
That's interesting. Why?
|
Mark M. |
everything is set up in code -- there are no more layout resources, style resources, etc.
|
Tad F. |
OMG! I'm very excited about that. I've never been all that comfortable with that split.
|
Mark M. |
there are still objects for defining the theme, including typography settings, but you create those objects yourself
|
Tad F. |
I just put it down to me being too old and not willing to get with the program :)
|
Mark M. |
so adding a scale factor to the theme based on your SharedPreference (or whatever) should be fairly straightforward
|
Mark M. |
and the rest of your code just inherits it from the theme
|
Tad F. |
The whole styles thing in particular has seemed pretty messy to me, but again I thought I just didn't "get it" properly.
|
Mark M. |
no, styles pretty much suck
|
Tad F. |
I mean, I understand overriding specific items, etc. It just seemed cumbersome
|
Sep 24 | 7:50 PM |
Mark M. |
agreed
|
Sep 24 | 7:50 PM |
Mark M. |
Compose is aiming to simplify that, among other things
|
Mark M. |
Compose is probably a year away from being stable, though
|
Tad F. |
Maybe by then they will have fixed the persisted permissions 128 limitation ;)
|
Mark M. |
nah
|
Mark M. |
that's a framework thing
|
Mark M. |
lots easier to innovate with libraries
|
Tad F. |
I ended up having to do really silly things like copy data from within the same device to my private directory on the device.
|
Tad F. |
Waste
|
Sep 24 | 7:55 PM |
Tad F. |
And for data where that isn't an option (user has 20 playlists using content from YouTube, etc.), I just have to say 'too bad for you, you have to lose one of these to define your next one').
|
Tad F. |
Blech
|
Mark M. |
¯\_(ツ)_/¯
|
Tad F. |
It isn't that way 'ahem', on iOS, at least according to my colleagues who love to give me grief about Android :)
|
Tad F. |
But oh well - I've thoroughly enjoyed doing this project.
|
Tad F. |
Learned a lot, and its fun.
|
Tad F. |
You and your content have been a big help
|
Mark M. |
happy to be useful!
|
Tad F. |
OK I'm going to sign of for today, thanks again as always!
|
Mark M. |
you're welcome!
|
Mark M. |
have a pleasant day!
|
Sep 24 | 8:10 PM |
Tad F. | has left the room |
Sep 24 | 8:25 PM |
Mark M. | turned off guest access |