mark cerqueira well-rounded nerd

Android SDK Trickery

Sometimes you need to write cringe-inducing code. The other day, I was trying to figure out when the Android keyboard was present and not present. Unlike iOS, Android doesn’t provide convenient ways to detect when the keyboard appears and disappears. I found a “solution” that keeps track of the size of the root view of the activity; if its display frame is smaller than the actual height of the view, a keyboard is likely present. To detect this I added a global layout listener to my root view’s view tree. When I went to remove it, I ended up having to write this:

@SuppressWarnings("deprecation") // no words for this sadness...
private void stopListeningForKeyboardDismissal() {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(mKeyboardPresentListener);
    } else {
        mRootView.getViewTreeObserver().removeGlobalOnLayoutListener(mKeyboardPresentListener);
    }
}

The method removeGlobalOnLayoutListener was introduced in API level 1, but it did not follow the naming conventions in the ViewTreeObserver class. So it was deprecated and replaced by removeOnGlobalLayoutListener in API level 16. What would happen if you didn’t call these properly? Just a NoSuchMethodError exception that would cause much sadness. Good troll, Google. Good troll.

The Androge