Read font size from Settings

This question is a almost dublicate of 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.

  • Get URI of .mp3 file stored in res/raw folder in android
  • Android 5.0 - ProgressBar cannot be displayed over a Button
  • App is compiled every time Gradle runs, taking significant time
  • Estimote beacons - Difference between monitoring and ranging listeners
  • Eclipse error “ADB server didn't ACK, failed to start daemon”
  • IN clause and placeholders
  • 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.

    Related posts:

    Send HTTP GET request with header
    Android: Entire ListView changes color on focus, not just ListView child item
    How to use Call Type with EventBus
    how to store JSON object in SQLite database
    How to download a pdf file in Android?
    Get size of Bitmap Android
  • Geting smaller buckets from GoogleFit doesn't work
  • Android Widget update
  • android: Recursive copy with adb push
  • How to Draw TextView on Canvas in android..?
  • How do I check if system is 12 or 24 hour?
  • Push up content except some view when keyboard shown
  • 4 Solutions collect form web for “Read font size from Settings”

    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 cound fix my code by using:

    float scale = getResources().getConfiguration().fontScale;
    

    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);
    

    You could probably use getResources().getDisplayMetrics().scaledDensityhttp://developer.android.com/reference/android/util/DisplayMetrics.html#scaledDensity

    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);

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.