newdev92
newdev92

Reputation: 111

Detect if 'High contrast' is enabled in Android accessibility settings

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

Upvotes: 10

Views: 4975

Answers (3)

anuj kumar
anuj kumar

Reputation: 31

we can check highContrast fonts like this

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

Upvotes: 3

Bhaven Shah
Bhaven Shah

Reputation: 692

  • @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.

Upvotes: 4

alxscms
alxscms

Reputation: 3067

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

Upvotes: 5

Related Questions