Java Code Examples for android.view.View#setOnApplyWindowInsetsListener()

The following examples show how to use android.view.View#setOnApplyWindowInsetsListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TranslucentModalHostView.java    From react-native-modal-translucent with MIT License 6 votes vote down vote up
public static void setStatusBarTranslucent(Window window, boolean translucent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        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);
    }
}
 
Example 2
Source File: AppUtils.java    From AndroidNavigation with MIT License 6 votes vote down vote up
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((v, 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());
    }
}
 
Example 3
Source File: BaseActivity.java    From DKVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 把状态栏设成透明
 */
protected void setStatusBarTransparent() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        View decorView = getWindow().getDecorView();
        decorView.setOnApplyWindowInsetsListener((v, insets) -> {
            WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
            return defaultInsets.replaceSystemWindowInsets(
                    defaultInsets.getSystemWindowInsetLeft(),
                    0,
                    defaultInsets.getSystemWindowInsetRight(),
                    defaultInsets.getSystemWindowInsetBottom());
        });
        ViewCompat.requestApplyInsets(decorView);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
    }
}
 
Example 4
Source File: DetailActivity.java    From google-io-2014 with Apache License 2.0 6 votes vote down vote up
private void applySystemWindowsBottomInset(int container) {
    View containerView = findViewById(container);
    containerView.setFitsSystemWindows(true);
    containerView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
            DisplayMetrics metrics = getResources().getDisplayMetrics();
            if (metrics.widthPixels < metrics.heightPixels) {
                view.setPadding(0, 0, 0, windowInsets.getSystemWindowInsetBottom());
            } else {
                view.setPadding(0, 0, windowInsets.getSystemWindowInsetRight(), 0);
            }
            return windowInsets.consumeSystemWindowInsets();
        }
    });
}
 
Example 5
Source File: ShapeWear.java    From ETSMobile-Android2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
        }
    });
}
 
Example 6
Source File: DrawerLayoutCompatApi21.java    From u2020 with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 7
Source File: ViewUtils.java    From animation-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Applies top window insets for a view.
 *
 * @param view The view to apply insets for.
 */
public static void applyTopWindowInsetsForView(@NonNull final View view) {
    view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            v.setPadding(v.getPaddingLeft(), insets.getSystemWindowInsetTop()
                    + v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());
            return insets;
        }
    });
    view.requestApplyInsets();
}
 
Example 8
Source File: DrawerLayoutCompatApi21.java    From debugdrawer with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 9
Source File: DrawerLayoutCompatApi21.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: DrawerLayout.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
@Override
public void configureApplyInsets(View drawerLayout) {
	if (drawerLayout instanceof DrawerLayout) {
		drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
		drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
				| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
	}
}
 
Example 11
Source File: ShapeWear.java    From WearViewStub with Apache License 2.0 5 votes vote down vote up
/**
 * Initialized to determine screen shape
 * @param view
 */
private static void initShapeDetection(View view){
    if(!setOnApplyWindowInsetsListenerCalled) {
        setOnApplyWindowInsetsListenerCalled = true;
        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 (onShapeChangeListeners != null && !onShapeChangeListeners.isEmpty()) {
                    synchronized (onShapeChangeListeners) {
                        for (OnShapeChangeListener listener : onShapeChangeListeners)
                            listener.shapeDetected(getShape());
                    }
                }
                return insets;
            }
        });
        view.requestApplyInsets();
    }
}
 
Example 12
Source File: DrawerLayoutCompatApi21.java    From 920-text-editor-v2 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 13
Source File: ShapeDetector.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
/**
 * 检测手表端设备屏幕形状
 *
 * @param view     用于检测的view
 * @param callback 检测结果的回调
 */
@TargetApi(20)
public static void detectShapeOnWatch(View view, final ShapeDetectCallback callback) {

    if (callback != null && shape != SHAPE_UNKNOWN) {
        callback.onShapeDetected(shape);

        return;
    }

    if (Build.VERSION.SDK_INT < 20) {
        if (callback != null) {
            callback.onShapeDetected(SHAPE_SQUARE);
        }
        return;
    }

    // Added in API level 20
    view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

            if (insets.isRound()) {
                shape = SHAPE_ROUND;

            } else {
                shape = SHAPE_SQUARE;
            }

            if (callback != null) {
                callback.onShapeDetected(shape);
            }
            return insets;
        }
    });
}
 
Example 14
Source File: LicensesActivity.java    From zephyr with MIT License 5 votes vote down vote up
private void setupEdgeToEdgeLayout() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    decorView.setOnApplyWindowInsetsListener((v, insets) -> {
        mToolbar.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0);
        return insets.consumeSystemWindowInsets();
    });
}
 
Example 15
Source File: MainActivity.java    From zephyr with MIT License 5 votes vote down vote up
private void setupEdgeToEdgeLayout() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    decorView.setOnApplyWindowInsetsListener((v, insets) -> {
        CoordinatorLayout.LayoutParams mainFragmentLayoutParams = new CoordinatorLayout.LayoutParams(mMainFragment.getLayoutParams());
        mainFragmentLayoutParams.setMargins(0, insets.getSystemWindowInsetTop(), 0, 0);
        mMainFragment.setLayoutParams(mainFragmentLayoutParams);
        return insets;
    });
}
 
Example 16
Source File: NotificationActivity.java    From zephyr with MIT License 5 votes vote down vote up
private void setupEdgeToEdgeLayout() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    decorView.setOnApplyWindowInsetsListener((v, insets) -> {
        AppBarLayout.LayoutParams toolbarLayoutParams = new AppBarLayout.LayoutParams(mToolbar.getLayoutParams());
        toolbarLayoutParams.setMargins(0, insets.getSystemWindowInsetTop(), 0, 0);
        mToolbar.setLayoutParams(toolbarLayoutParams);
        return insets;
    });
}
 
Example 17
Source File: StatusBarUtils.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
public static void getStatusBarHeight(final View view, final StatusBarHeightListener listener) {
    if (STATUS_BAR_HEIGHT > 0) {
        listener.onStatusBarHeightFetched(STATUS_BAR_HEIGHT);
    }

    view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            STATUS_BAR_HEIGHT = insets.getSystemWindowInsetTop();
            listener.onStatusBarHeightFetched(STATUS_BAR_HEIGHT);
            return insets;
        }
    });
}