This question is related to Android App: how to read Font Size under Settings? I read the answer of CommonsWare which points to use Settings.System.FONT_SCALE
. So I wrote this few lines:
float scale = -66.6f;
try {
scale = android.provider.Settings.System.getFloat(getContentResolver(),
android.provider.Settings.System.FONT_SCALE);
} catch(SettingNotFoundException e) {
Log.e(getClass().getSimpleName(), "Error: ", e);
}
Toast.makeText(this, "scale=" + scale, Toast.LENGTH_LONG).show();
My problem is that I always get the SettingNotFoundException
I'm using an Android 4.2.2 device. By the way I that code is in an onCreate
callback of an Activity
.
I also googled for that problem and found about 3 sites which uses the same code. Is some special permission required? I tried also the permission android.permission.WRITE_SETTINGS
without success.
The calculation of font size as prescribed in DIN 1450 is based on x-heights. Font size (here x-height) is generally set depending on the viewer's distance from it. The shorter a viewer's distance from a text is, the smaller its font can be and vice-versa.
How do I get system font size in react native? on Android value reflects the user preference set in Settings > Display > Font size. on iOS value reflects the user preference set in Settings > Display & Brightness > Text Size, value can also be updated in Settings > Accessibility > Display & Text Size > Larger Text.
1. Font scaling is an alternate term used to describe a scalable font or vector font. 2. Font scaling is a printer feature that allows the font to be scaled by the printer instead of in the document program.
1 pixel (px) is usually assumed to be 1/96th of an inch. 1 point (pt) is assumed to be 1/72nd of an inch. Therefore 16px = 12pt.
I just found in the source code of Settings.System
this function:
/** @hide */
public static void getConfigurationForUser(ContentResolver cr,
Configuration outConfig, int userHandle) {
outConfig.fontScale = Settings.System.getFloatForUser(
cr, FONT_SCALE, outConfig.fontScale, userHandle);
if (outConfig.fontScale < 0) {
outConfig.fontScale = 1;
}
}
There is however the FONT_SCALE
in usage so I checked for that Configuration
class where the documentation points to getResources().getConfiguration()
. So I counld fix my code by using:
float scale = getResources().getConfiguration().fontScale;
Since my question was about to calculate the correct font size in pixel here is the way I use it nowerdays in Kotlin:
val Number.dpInPx: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, toFloat(), Resources.getSystem().displayMetrics).toInt()
val Number.spInPx: Int
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, toFloat(), Resources.getSystem().displayMetrics).toInt()
The usage is:
val textSize = 42.spInPx
val padding = 8.dpInPx
You can set system font according to font scale.
Example: For huge font scale is 2.0 then you can set as follow.
Configuration config = new Configuration();
config.fontScale = 2.0f;
getResources().getConfiguration().setTo(config);
Add "float def" param to the end of the
public static float getFloat(ContentResolver cr, String name, float def)
Example:
scale = android.provider.Settings.System.getFloat(getActivity().getContentResolver(),
android.provider.Settings.System.FONT_SCALE, 1f);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With