How to use different set of text sizes in android? - android

In my app, i need to keep an action to change the font size. I am trying to keep three set of values for text sizes (small,medium and large). How should i switch between them? My idea is to use one set of font resource at a time.
For more clarification:
In Theme.AppCompat.DayNight.DarkActionBar, to shift between night mode on and off in i can call
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
and
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Here, each will use different set of resource values for night mode on and off. I am looking for somewhat such a method to shift between different font sizes. Any help is appreciated.

There's something like that built into the operating system. If you go to Settings > Accessibility you'll find a Large text toggle or a slider to make text bigger (depending on phone). This will work in your app as long as you use sp for your font sizes (for example android:textSize="16sp").
If you're going to use that, you'll have to remember that the setting is in the phone settings, I don't think there's a way for you to change it from your app. And it applies to all the apps, not just to yours, so everything in the system would get bigger. Which might be not what you require.
Otherwise you can try to do this manually in your app. One way would be to create three themes that have your different font sizes. Then you would set an appropriate theme on the context in your Activity.onCreate() based the setting you saved somewhere (in SharedPreference for example).
But I don't think you can just keep different font sizes in different resource subdirectories. The list of resource qualifiers is predefined and I don't think there's something for switching font sizes in there.

Related

How to view preview of text size of user font in Android Studio?

When specifying text size on Android, the suggested the best practice is to use sp font units instead of dp, since with sp the text will increase/decrease in size according to the user's display preferences. For example, on my device I can go to
Settings -> Display -> Font
and choose a preference: small, normal, large, huge (these seem to vary across devices though).
Does anyone know a way to preview the effect of this preference in Android Studio's layout preview? It would be easier than having to deploy to a device/emulator in order to test. I can't see an option for this, but maybe I'm missing it.
Related: is there a standard multiplier for what the largest ("huge") font preference equals to? Or, can this even vary across devices? If there's no upper bound to how large the text could be, the first part of my question is probably moot.
As of about May 2020, This can now be done via the Layout Validation feature of Android Studio 4.0. This allows you to view different Font Sizes, devices and even see your layouts from the perspective of someone with color blindness.
No, there is no such way to preview the font scaling in the android studio as of now. but you can check out the material design for text scaling at < https://material.io/design/typography/the-type-system.html#type-scale > here you will some idea of how your text looks in runtime

Android: Change layout based on phone's default text size

Is there any way to change the layout of your app based on what text size the phone is set to? I know you can set your text so it will be static (stay the same size) but I've had some users complain because the app doesn't look good because of the large text. These are mainly older people who need the large text and it would be nice if there was an easy way to accomplish this without splitting one activity into two separate ones so they could still use it. Any ideas?

Android: Can I get the Users Font Size preference Programatically?

I want to use SP when defining my fonts so that they scale according to my User's needs. However one screen which I have created looks great when on Normal or Smaller size but wraps text and looks messy on Large size.
So where I have text which has two label/value pairs of information going across the screen, which looked great on Normal, I need to change this to having one pair on each line so that this stops looking messy on Large.
But if I now switch back to Normal or smaller size this does not look so good and not as nicely spaced as before,
So...what I would really like to do is have two layouts, one which I use if the user has a Large+ and a different one for Normal or smaller. To get this to work I need to be able to get the User's currently selected Font Preference.
Can anyone help, I cannot find any reference to how to do this?

Android, Change font size only in one app

Is it possible to change the default-fontsize only for one app?
So i can use the "sp" units at all my fonts?
The problem is that my customer doesn´t wan´t that his employes changes anything in the android-settings. So i am searching for a solution to change the font size in my app. Of course i could implement my own dynamic font size by programmatically changing the size of each label when the user chooses another size. But i think that is to complicated and should by the last way.
Best regards
Yannick

Determining user font size selection

I have an Android app that needs some adjustment if the user sets their font size to extra large (via Settings -> Display -> Font size in 4.0 and higher).
Is there a simple way for me to tell what the user's font size preference is
Updated:
in my layout.xml I have lines similar to to setup a button
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:typeface="sans"
Notice that I'm not setting the font size directly. This layout works and looks good in all cases, except for the Extra Large setting. In that case, due to space limitations, it causes the button's text to wrap to 2 lines.
My goal is to make a slight wording change in the case of Extra Large so that it doesn't wrap
There's a FONT_SCALE parameter you should be able to query the system for. I haven't used it myself, but I imagine retrieving its value would look somewhat like this:
float fontScale = Settings.System.getFloat(context.getContentResolver(), Settings.System.FONT_SCALE)
However, I'd also like to point out that usually you shouldn't be dealing with this value directly. In stead, use sp units for textual content so that you don't have to worry about adjusting to user-preferred font sizes yourself, but rather let the system handle that.
Also refer to: Why should we use sp for font sizes in Android?

Categories

Resources