Skip to content Skip to sidebar Skip to footer

GetWindow().hasFeature() On API < 11

I need to check if the overlay feature has been set on an ActionBarCompat instance. The getWindow().hasFeature() method is only available on API 11 and up. How can I check the fea

Solution 1:

You can access private methods using The Reflection API.

boolean hasFeature(int feature) {
    Window window = getWindow(); //get the window instance.
    if (android.os.Build.VERSION.SDK_INT >= 11) { // if we are running api level 11 and later
        return window.hasFeature(feature); //call hasFeature
    } else {
        try {
            Class c = window.getClass();
            Method getFeatures = c.getDeclaredMethod("getFeatures");//get the getFeatures method using reflection
            getFeatures.setAccessible(true);//make it public
            Integer features = getFeatures.invoke(window, null); //invoke it
            return (features.intValue() & (1 << feature)) != 0; //check if we have the feature and return the result.
        } catch (Exception e) {
            return false;//in case invocation fails with any reason
        }
    }
}

Post a Comment for "GetWindow().hasFeature() On API < 11"