While developing my app I realised if someone changes device font size from normal, my application font sizes change too and it destroys some of the visuals I designed. So I wanted to ask if there is a way to fix text sizes independent from device's settings?
Thanks
check the source code DisplaySettings.java, here is the method to control font size scale
public void writeFontSizePreference(Object objValue) {
try {
mCurConfig.fontScale = Float.parseFloat(objValue.toString());
ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
} catch (RemoteException e) {
Log.w(TAG, "Unable to save font size");
}
}
here is a solution keep the font size in different fontscale.
float textSize = 22f;
mTextView.setTextSize(textSize / getResources().getConfiguration().fontScale);
and I think @shoe rat's answer is more better for an activity or application context.
If you want to completely ignore user's font size preferences, then use dp
instead of sp
for font sizes. The lint will complain about the dp
usage (and rightly so, because you are potentially causing inconvenience to the user and possibly rendering your app unusable for visually impaired ones), but you should not face any runtime issues.
If you just want to ignore runtime font size changes, use "fontScale":
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
... >
<activity android:name=".MyActivity"
android:configChanges="fontScale">
...
</activity>
</application>
Google recommends keeping your font sizes set with SP so that the UI settings will modify the font sizes within your application. Are you using SP?
I believe that using DP or PX or something else will cure your issue. Keep in mind that this is not recommended for Accessibility purposes.
For a complete list of possible text size types, see http://developer.android.com/reference/android/util/TypedValue.html
Here they are:
COMPLEX_UNIT_DIP - TYPE_DIMENSION complex unit: Value is Device Independent Pixels.
COMPLEX_UNIT_IN - TYPE_DIMENSION complex unit: Value is in inches.
COMPLEX_UNIT_MM - TYPE_DIMENSION complex unit: Value is in millimeters.
COMPLEX_UNIT_PT - TYPE_DIMENSION complex unit: Value is in points.
COMPLEX_UNIT_PX - TYPE_DIMENSION complex unit: Value is raw pixels.
COMPLEX_UNIT_SP - TYPE_DIMENSION complex unit: Value is a scaled pixel.
Also: Note that not all of these options are available from an xml file and that some of these settings may only be used programmatically (at least that's what I recall).