8

How can I detect if 'High Contrast' setting (available on Android 5.0+) is enabled in Accessibility settings?

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
newdev92
  • 91
  • 1
  • 3

3 Answers3

5

In the AccessibilityManager class (see source here) you have a public method called isHighTextContrastEnabled that you can use to get your information:

/**
 * Returns if the high text contrast in the system is enabled.
 * <p>
 * <strong>Note:</strong> You need to query this only if you application is
 * doing its own rendering and does not rely on the platform rendering pipeline.
 * </p>
 *
 * @return True if high text contrast is enabled, false otherwise.
 *
 * @hide
 */
public boolean isHighTextContrastEnabled() {
    synchronized (mLock) {
        IAccessibilityManager service = getServiceLocked();
        if (service == null) {
            return false;
        }
        return mIsHighTextContrastEnabled;
    }
}

So in your code, you can access this method by doing so (if you're in an Activity):

AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();
alxscms
  • 3,067
  • 6
  • 22
  • 43
4
  • @alxscms' answer may be right but it does not help me So I found an alternative way to check High contrast text is Enabled or not in android.

Below function will return true if HighContrastText is enabled in user phone and otherwise return false.

Below function is checked in all android phones and it's working.

public static boolean isHighContrastTextEnabled(Context context) {
    if (context != null) {
        AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        Method m = null;

        if (am != null) {
            try {
                m = am.getClass().getMethod("isHighTextContrastEnabled", null);
            } catch (NoSuchMethodException e) {
                Log.i("FAIL", "isHighTextContrastEnabled not found in AccessibilityManager");
            }
        }

        Object result;
        if (m != null) {
            try {
                result = m.invoke(am, null);
                if (result instanceof Boolean) {
                    return (Boolean) result;
                }
            } catch (Exception e) {
                Log.i("fail", "isHighTextContrastEnabled invoked with an exception" + e.getMessage());
            }
        }
    }
    return false;
}

I hope this can help many more others.

Bhaven Shah
  • 692
  • 1
  • 5
  • 18
3

we can check highContrast fonts like this

public boolean isHighTextContrastEnabled(Context context) {
    return Settings.Secure.getInt(context.getContentResolver(), "high_text_contrast_enabled", 0) == 1;
}
anuj kumar
  • 31
  • 1