Raneez Ahmed
Raneez Ahmed

Reputation: 3828

Scaling Font Size According to Android Accessibility Settings - Handling Thresholds

Hello Android developers,

I'm currently working on an Android app and I'm keen on ensuring that it complies with the American Disabilities Act (ADA) by allowing users to scale the font size based on their preferences set in the Android system settings. However, I've encountered a challenge when it comes to accommodating users who prefer an extra-large font size.

On certain devices, it seems that the scale factor for the highest font size setting is set to 1.3f, but on other devices, it appears to be even higher. This discrepancy is causing issues because when the font size is scaled up too much, it breaks the app's user interface (UI).

I'd like to strike a balance between accessibility and maintaining the integrity of the UI. Is there a recommended approach or best practice for setting a threshold on font size scaling in Android apps?

Here is the sample code I tried :

override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase)
    val newOverride = Configuration(newBase?.resources?.configuration)
    if(newOverride.fontScale > threshold) {
     newOverride.fontScale = threshold
    }
    applyOverrideConfiguration(newOverride)

}

Here are some specific questions I have:

  1. How can I programmatically determine the maximum font scaling factor that the Android system allows for text size adjustments?
  2. Are there any guidelines or recommendations for establishing a threshold beyond which font size scaling should be restricted to prevent UI issues?
  3. Is there a way to detect when the font size scaling reaches a certain threshold and then implement UI adjustments accordingly to maintain a cohesive user experience?
  4. Is it possible to adjust the font size for each fragments rather than applying to whole activity?

I'd appreciate any insights, tips, or code examples that could help me handle this situation effectively. Thank you in advance for your assistance!

Upvotes: 0

Views: 1689

Answers (1)

Jai Goyal
Jai Goyal

Reputation: 1

here is how you can modify your code. It's recommended to set a conservative threshold based on your app's design and user testing. I have used 1.3f factor as it is best as per me. you can play around with it.

protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    adjustFontScalingThreshold();
}

private void adjustFontScalingThreshold() {
    Configuration configuration = getResources().getConfiguration();

    // Define your desired font scaling threshold here
    float maxFontScaleThreshold = 1.3f;

    // Check if font scaling is greater than the threshold
    if (configuration.fontScale > maxFontScaleThreshold) {
        configuration.fontScale = maxFontScaleThreshold;

        // Create a new context with modified configuration
        Context adjustedContext = createConfigurationContext(configuration);

        // Apply the adjusted context as the base context
        attachBaseContext(adjustedContext);
    }
}

Also just to give you more hints and ideas.. You can use the getScaledDensity() method of the DisplayMetrics to get the density scale factor, which indirectly affects font scaling.

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float densityScaleFactor = displayMetrics.scaledDensity;

Note that this factor is related to the overall density and scaling of the display, and it might not directly correspond to font scaling thresholds.

Upvotes: 0

Related Questions