androidx.core.view.OnApplyWindowInsetsListener Java Examples

The following examples show how to use androidx.core.view.OnApplyWindowInsetsListener. 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: MaterialDialogDecorator.java    From AndroidMaterialDialog with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and returns a listener, which allows to observe when window insets are applied to the
 * root view of the view hierarchy, which is modified by the decorator.
 *
 * @return The listener, which has been created, as an instance of the type {@link
 * OnApplyWindowInsetsListener}
 */
private OnApplyWindowInsetsListener createWindowInsetsListener() {
    return new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                      final WindowInsetsCompat insets) {
            systemWindowInsets = insets.hasSystemWindowInsets() ?
                    new Rect(insets.getSystemWindowInsetLeft(),
                            insets.getSystemWindowInsetTop(),
                            insets.getSystemWindowInsetRight(),
                            insets.getSystemWindowInsetBottom()) : null;
            adaptLayoutParams();
            return insets;
        }

    };
}
 
Example #2
Source File: MainActivity.java    From ChromeLikeTabSwitcher with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a listener, which allows to apply the window insets to the tab switcher's padding.
 *
 * @return The listener, which has been created, as an instance of the type {@link
 * OnApplyWindowInsetsListener}. The listener may not be nullFG
 */
@NonNull
private OnApplyWindowInsetsListener createWindowInsetsListener() {
    return new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                      final WindowInsetsCompat insets) {
            int left = insets.getSystemWindowInsetLeft();
            int top = insets.getSystemWindowInsetTop();
            int right = insets.getSystemWindowInsetRight();
            int bottom = insets.getSystemWindowInsetBottom();
            tabSwitcher.setPadding(left, top, right, bottom);
            float touchableAreaTop = top;

            if (tabSwitcher.getLayout() == Layout.TABLET) {
                touchableAreaTop += getResources()
                        .getDimensionPixelSize(R.dimen.tablet_tab_container_height);
            }

            RectF touchableArea = new RectF(left, touchableAreaTop,
                    getDisplayWidth(MainActivity.this) - right, touchableAreaTop +
                    ThemeUtil.getDimensionPixelSize(MainActivity.this, R.attr.actionBarSize));
            tabSwitcher.addDragGesture(
                    new SwipeGesture.Builder().setTouchableArea(touchableArea).create());
            tabSwitcher.addDragGesture(
                    new PullDownGesture.Builder().setTouchableArea(touchableArea).create());
            return insets;
        }

    };
}
 
Example #3
Source File: DynamicViewUtils.java    From dynamic-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Apply window insets padding for the supplied view.
 *
 * @param view The view to set the insets padding.
 * @param left {@code true} to apply the left window inset padding.
 * @param top {@code true} to apply the top window inset padding.
 * @param right {@code true} to apply the right window inset padding.
 * @param bottom {@code true} to apply the bottom window inset padding.
 * @param consume {@code true} to consume the applied window insets.
 */
public static void applyWindowInsets(@Nullable View view, final boolean left,
        final boolean top, final boolean right, final boolean bottom, final boolean consume) {
    if (view == null) {
        return;
    }

    final int paddingLeft = view.getPaddingLeft();
    final int paddingTop = view.getPaddingTop();
    final int paddingRight = view.getPaddingRight();
    final int paddingBottom = view.getPaddingBottom();

    ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(left ? paddingLeft + insets.getSystemWindowInsetLeft(): paddingLeft,
                    top ? paddingTop + insets.getSystemWindowInsetTop() : paddingTop,
                    right ? paddingRight + insets.getSystemWindowInsetRight() : paddingRight,
                    bottom ? paddingBottom + insets.getSystemWindowInsetBottom() : paddingBottom);

            return !consume ? insets :
                    new WindowInsetsCompat.Builder(insets).setSystemWindowInsets(
                            Insets.of(left ? 0 : insets.getSystemWindowInsetLeft(),
                                    top ? 0 : insets.getSystemWindowInsetTop(),
                                    right ? 0 : insets.getSystemWindowInsetRight(),
                                    bottom ? 0 : insets.getSystemWindowInsetBottom()))
                            .build();
        }
    });

    requestApplyWindowInsets(view);
}
 
Example #4
Source File: BaseActivity.java    From OneText_For_Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getSharedPreferences("setting", MODE_PRIVATE);
    editor = sharedPreferences.edit();
    //Q导航栏沉浸
    rootview = findViewById(android.R.id.content);

    /*ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(0,0,0,insets.getSystemWindowInsetBottom());
            return insets;
        }
    });*/
    //状态栏icon黑色
    int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    if (mode == Configuration.UI_MODE_NIGHT_NO) {
        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
    }
    rootview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    ViewCompat.setOnApplyWindowInsetsListener(rootview, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            rootview.setPadding(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), 0);
            return insets;
        }
    });
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        //rootView.setFitsSystemWindows(true);
        //rootView.setPadding(0,0,0,getNavigationBarHeight());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    //rootView.setFitsSystemWindows(true);
    /*if ((Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)|(getNavigationBarHeight()>dp2px(16))) {
        //rootView.setPadding(0,0,0,getNavigationBarHeight());
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
            //rootView.setFitsSystemWindows(true);
            rootView.setPadding(0,0,0,getNavigationBarHeight());
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }else {
            //rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            //rootView.setFitsSystemWindows(true);
        }
    }else {
        rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }*/
    //设置为miui主题
    //setMiuiTheme(BaseActivity.this,0,mode);
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    //全局自定义字体
    ViewPump.init(ViewPump.builder()
            .addInterceptor(new CalligraphyInterceptor(
                    new CalligraphyConfig.Builder()
                            .setDefaultFontPath(sharedPreferences.getString("font_path", null))
                            .setFontAttrId(R.attr.fontPath)
                            .build()))
            .build());
}
 
Example #5
Source File: DynamicSystemActivity.java    From dynamic-support with Apache License 2.0 4 votes vote down vote up
/**
 * Set the navigation bar color.
 * <p>It will be applied only on the API 21 and above devices.
 *
 * @param color The color to be applied.
 */
public void setNavigationBarColor(@ColorInt int color) {
    if (DynamicTheme.getInstance().get().isBackgroundAware()
            && !DynamicSdkUtils.is26()) {
        color = DynamicColorUtils.getContrastColor(color, ADS_DEFAULT_SYSTEM_UI_COLOR);
    }

    int orientation = DynamicWindowUtils.getScreenOrientation(this);
    if (DynamicWindowUtils.isNavigationBarThemeSupported(this)
            && (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            || orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE)) {
        if (!setNavigationBarThemeInLandscape()) {
            color = ADS_DEFAULT_SYSTEM_BG_COLOR;
        }
    }

    this.mNavigationBarColor = color;
    if (DynamicSdkUtils.is21()) {
        this.mNavigationBarTheme = setNavigationBarTheme();

        if (isEdgeToEdgeContent()) {
            if ((getWindow().getDecorView().getSystemUiVisibility()
                    & View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
                    != View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) {
                DynamicViewUtils.setEdgeToEdge(getWindow().getDecorView(), true);
            }

            if (isApplyEdgeToEdgeInsets() && getEdgeToEdgeView() != null) {
                ViewCompat.setOnApplyWindowInsetsListener(getEdgeToEdgeView(),
                        new OnApplyWindowInsetsListener() {
                            @Override
                            public WindowInsetsCompat onApplyWindowInsets(
                                    View v, WindowInsetsCompat insets) {
                                final ViewGroup.MarginLayoutParams lp =
                                        (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                                lp.topMargin = insets.getSystemWindowInsetTop();

                                v.setLayoutParams(lp);
                                DynamicViewUtils.applyWindowInsetsBottom(
                                        getEdgeToEdgeViewBottom());

                                return insets.consumeSystemWindowInsets();
                            }
                        });
            }

            mAppliedNavigationBarColor = Color.TRANSPARENT;
            getWindow().setNavigationBarColor(mAppliedNavigationBarColor);
        } else {
            mAppliedNavigationBarColor = color = mNavigationBarTheme
                    ? color : ADS_DEFAULT_SYSTEM_BG_COLOR;
            getWindow().setNavigationBarColor(color);
        }
    } else {
        mAppliedNavigationBarColor = mNavigationBarColor;
    }

    updateNavigationBar();
}
 
Example #6
Source File: DynamicViewUtils.java    From dynamic-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Apply window insets margin for the supplied view.
 *
 * @param view The view to set the insets margin.
 * @param left {@code true} to apply the left window inset margin.
 * @param top {@code true} to apply the top window inset margin.
 * @param right {@code true} to apply the right window inset margin.
 * @param bottom {@code true} to apply the bottom window inset margin.
 * @param consume {@code true} to consume the applied window margin.
 */
public static void applyWindowInsetsMargin(@Nullable View view, final boolean left,
        final boolean top, final boolean right, final boolean bottom, final boolean consume) {
    if (view == null) {
        return;
    }

    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        final ViewGroup.MarginLayoutParams layoutParams =
                (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        final int marginLeft = layoutParams.leftMargin;
        final int marginTop = layoutParams.topMargin;
        final int marginRight = layoutParams.rightMargin;
        final int marginBottom = layoutParams.bottomMargin;

        ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                if (left) {
                    layoutParams.leftMargin = marginLeft
                            + insets.getSystemWindowInsetLeft();
                }
                if (top) {
                    layoutParams.topMargin = marginTop
                            + insets.getSystemWindowInsetTop();
                }
                if (right) {
                    layoutParams.rightMargin = marginRight
                            + insets.getSystemWindowInsetRight();
                }
                if (bottom) {
                    layoutParams.bottomMargin = marginBottom
                            + insets.getSystemWindowInsetBottom();
                }

                return !consume ? insets :
                        new WindowInsetsCompat.Builder(insets).setSystemWindowInsets(
                                Insets.of(left ? 0 : insets.getSystemWindowInsetLeft(),
                                        top ? 0 : insets.getSystemWindowInsetTop(),
                                        right ? 0 : insets.getSystemWindowInsetRight(),
                                        bottom ? 0 : insets.getSystemWindowInsetBottom()))
                                .build();
            }
        });
    }
}
 
Example #7
Source File: ViewPagerActivity.java    From FlexibleAdapter with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_pager);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    HeaderView headerView = (HeaderView) findViewById(R.id.toolbar_header_view);
    headerView.bindTo(getString(R.string.app_name), getString(R.string.viewpager));

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    //Coordinatorlayout Status Bar Padding Disappears From Viewpager 2nd-page
    //http://stackoverflow.com/questions/31368781/coordinatorlayout-status-bar-padding-disappears-from-viewpager-2nd-page
    ViewCompat.setOnApplyWindowInsetsListener(mViewPager, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v,
                                                      WindowInsetsCompat insets) {
            insets = ViewCompat.onApplyWindowInsets(v, insets);
            if (insets.isConsumed()) {
                return insets;
            }

            boolean consumed = false;
            for (int i = 0, count = mViewPager.getChildCount(); i < count; i++) {
                ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets);
                if (insets.isConsumed()) {
                    consumed = true;
                }
            }
            return consumed ? insets.consumeSystemWindowInsets() : insets;
        }
    });
}