Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamikat/d0ae9eb13353538ebf5d to your computer and use it in GitHub Desktop.
Save kamikat/d0ae9eb13353538ebf5d to your computer and use it in GitHub Desktop.
AndroidBug5497Workaround supporting translucent status window attribute
package com.tietie.foundation.util;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class AndroidBug5497WorkaroundSupportingTranslucentStatus {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497WorkaroundSupportingTranslucentStatus(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private boolean isTranslucentEnabled = false;
private AndroidBug5497WorkaroundSupportingTranslucentStatus(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
final WindowManager.LayoutParams attrs = activity.getWindow()
.getAttributes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
isTranslucentEnabled = (attrs.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) != 0;
}
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
if (isTranslucentEnabled) {
r.top = 0;
}
return (r.bottom - r.top);
}
}
@asamaru7
Copy link

add case:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if ((!isTranslucentEnabled) && (activity.getWindow().getStatusBarColor() == Color.TRANSPARENT)) {
                isTranslucentEnabled = true;
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment