public static void setOnApplyWindowInsetsListener(View view, final OnApplyWindowInsetsListener listener) { view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) { // Wrap the framework insets in our wrapper WindowInsetsCompatApi21 insets = new WindowInsetsCompatApi21(windowInsets); // Give the listener a chance to use the wrapped insets insets = (WindowInsetsCompatApi21) listener.onApplyWindowInsets(view, insets); // Return the unwrapped insets return insets.unwrap(); } }); } }
/** * Initialized to determine screen shape * @param view */ private static void initShapeDetection(View view){ view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { if (insets.isRound()) { shape = ScreenShape.ROUND; if(screenWidthPX == 320 && screenHeightPX == 290) { shape = ScreenShape.MOTO_ROUND; } } else { shape = ScreenShape.RECTANGLE; } if(onShapeChangeListener != null){ onShapeChangeListener.shapeDetected(getShape()); } return insets; } }); }
public static void configureApplyInsets(View drawerLayout) { if (drawerLayout instanceof DrawerLayoutImpl) { drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener()); drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } }
/** * Set a view to be resized when the keyboard is shown. This will set the bottom margin of the * view to be immediately above the keyboard, and assumes that the view sits immediately above * the navigation bar. * * <p>Note that you must set {@link android.R.attr#windowSoftInputMode} to {@code adjustResize} * for this class to work. Otherwise window insets are not dispatched and this method will have * no effect. * * <p>This will only take effect in versions Lollipop or above. Otherwise this is a no-op. * * @param view The view to be resized when the keyboard is shown. */ public static void setImeInsetView(final View view) { if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { view.setOnApplyWindowInsetsListener(new WindowInsetsListener()); } }
public static void setStatusBarTranslucent(Window window, boolean translucent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setRenderContentInShortEdgeCutoutAreas(window, translucent); View decorView = window.getDecorView(); if (translucent) { decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { WindowInsets defaultInsets = v.onApplyWindowInsets(insets); return defaultInsets.replaceSystemWindowInsets( defaultInsets.getSystemWindowInsetLeft(), 0, defaultInsets.getSystemWindowInsetRight(), defaultInsets.getSystemWindowInsetBottom()); } }); } else { decorView.setOnApplyWindowInsetsListener(null); } ViewCompat.requestApplyInsets(decorView); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (translucent) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } else { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } ViewCompat.requestApplyInsets(window.getDecorView()); } }
final View container = findViewById(R.id.preference_fragment_container); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) @Override public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) { toolbar.setPadding(toolbar.getPaddingStart() /*+ insets.getSystemWindowInsetLeft()*/, toolbar.getPaddingTop() + insets.getSystemWindowInsetTop(), toolbar.getPaddingEnd() /*+ insets.getSystemWindowInsetRight()*/, toolbar.getPaddingBottom()); aboutText.setPadding(aboutText.getPaddingStart(), aboutText.getPaddingTop(), aboutText.getPaddingEnd(), aboutText.getPaddingBottom() + insets.getSystemWindowInsetBottom()); View viewGroup = findViewById(R.id.swipeBackView); ViewGroup.MarginLayoutParams viewGroupParams = (ViewGroup.MarginLayoutParams) viewGroup.getLayoutParams(); viewGroupParams.leftMargin += insets.getSystemWindowInsetLeft(); viewGroupParams.rightMargin += insets.getSystemWindowInsetRight(); viewGroup.setLayoutParams(viewGroupParams); // clear this listener so insets aren't re-applied rootView.setOnApplyWindowInsetsListener(null); return insets.consumeSystemWindowInsets(); } });
rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) @Override
@Override @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) { toolbar.setPadding(toolbar.getPaddingStart() /*+ insets.getSystemWindowInsetLeft()*/, toolbar.getPaddingTop() + insets.getSystemWindowInsetTop(), toolbar.getPaddingEnd() /*+ insets.getSystemWindowInsetRight()*/, toolbar.getPaddingBottom()); ViewGroup.MarginLayoutParams toolbarParams = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); toolbarParams.leftMargin += insets.getSystemWindowInsetLeft(); toolbarParams.rightMargin += insets.getSystemWindowInsetRight(); toolbar.setLayoutParams(toolbarParams); container.setPadding(container.getPaddingStart() + insets.getSystemWindowInsetLeft(), container.getPaddingTop(), container.getPaddingEnd() + insets.getSystemWindowInsetRight(), container.getPaddingBottom() + insets.getSystemWindowInsetBottom()); // clear this listener so insets aren't re-applied rootView.setOnApplyWindowInsetsListener(null); return insets.consumeSystemWindowInsets(); } });