Reputation: 3828
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:
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
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