Skip to content

Instantly share code, notes, and snippets.

@SimonVT
Created June 4, 2015 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonVT/ad0290346ec6d25ffbb2 to your computer and use it in GitHub Desktop.
Save SimonVT/ad0290346ec6d25ffbb2 to your computer and use it in GitHub Desktop.
public class CatchSystemInsets extends FrameLayout {
private static final boolean CATCH_INSETS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
WindowInsets insets;
public CatchSystemInsets(Context context) {
super(context);
init();
}
public CatchSystemInsets(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CatchSystemInsets(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CatchSystemInsets(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
if (CATCH_INSETS) {
setFitsSystemWindows(true);
setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
@Override public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (CATCH_INSETS) {
this.insets = new WindowInsets(insets);
return insets.consumeSystemWindowInsets();
}
return insets;
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (CATCH_INSETS) {
if (insets != null) {
for (int i = 0, childCount = getChildCount(); i < childCount; i++) {
View child = getChildAt(i);
child.dispatchApplyWindowInsets(insets);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment