Search code examples
androidfontsfont-size

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


Solution

  • 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?