Search in sources :

Example 1 with WindowInsetsCompat

use of androidx.core.view.WindowInsetsCompat in project StatusBarCompat by niorgai.

the class StatusBarCompatLollipop method setStatusBarColorForCollapsingToolbar.

/**
 * compat for CollapsingToolbarLayout
 *
 * 1. change to full-screen mode(like translucentStatusBar).
 * 2. cancel CollapsingToolbarLayout's WindowInsets, let it layout as normal(now setStatusBarScrimColor is useless).
 * 3. set View's FitsSystemWindow to false.
 * 4. add Toolbar's height, let it layout from top, then add paddingTop to layout normal.
 * 5. change statusBarColor by AppBarLayout's offset.
 * 6. add Listener to change statusBarColor
 */
static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, final int statusColor) {
    Window window = activity.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            return insets;
        }
    });
    ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
        ViewCompat.setFitsSystemWindows(mChildView, false);
        ViewCompat.requestApplyInsets(mChildView);
    }
    ((View) appBarLayout.getParent()).setFitsSystemWindows(false);
    appBarLayout.setFitsSystemWindows(false);
    toolbar.setFitsSystemWindows(false);
    if (toolbar.getTag() == null) {
        CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
        int statusBarHeight = getStatusBarHeight(activity);
        lp.height += statusBarHeight;
        toolbar.setLayoutParams(lp);
        toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom());
        toolbar.setTag(true);
    }
    CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
    if (behavior != null && behavior instanceof AppBarLayout.Behavior) {
        int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset();
        if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
            window.setStatusBarColor(statusColor);
        } else {
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    } else {
        window.setStatusBarColor(Color.TRANSPARENT);
    }
    collapsingToolbarLayout.setFitsSystemWindows(false);
    final WeakReference<Window> windowWeakReference = new WeakReference<>(window);
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            Window weakWindow = windowWeakReference.get();
            if (weakWindow != null) {
                if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
                    if (weakWindow.getStatusBarColor() != statusColor) {
                        startColorAnimation(weakWindow.getStatusBarColor(), statusColor, collapsingToolbarLayout.getScrimAnimationDuration(), windowWeakReference);
                    }
                } else {
                    if (weakWindow.getStatusBarColor() != Color.TRANSPARENT) {
                        startColorAnimation(weakWindow.getStatusBarColor(), Color.TRANSPARENT, collapsingToolbarLayout.getScrimAnimationDuration(), windowWeakReference);
                    }
                }
            }
        }
    });
    collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);
    collapsingToolbarLayout.setStatusBarScrimColor(statusColor);
}
Also used : Window(android.view.Window) ViewGroup(android.view.ViewGroup) View(android.view.View) WindowInsetsCompat(androidx.core.view.WindowInsetsCompat) CoordinatorLayout(androidx.coordinatorlayout.widget.CoordinatorLayout) OnApplyWindowInsetsListener(androidx.core.view.OnApplyWindowInsetsListener) WeakReference(java.lang.ref.WeakReference) CollapsingToolbarLayout(com.google.android.material.appbar.CollapsingToolbarLayout) AppBarLayout(com.google.android.material.appbar.AppBarLayout)

Example 2 with WindowInsetsCompat

use of androidx.core.view.WindowInsetsCompat in project react-native-navigation by wix.

the class ComponentViewController method onApplyWindowInsets.

@Override
protected WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
    final Insets systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
    int systemWindowInsetTop = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top + insets.getInsets(WindowInsetsCompat.Type.navigationBars()).top - systemBarsInsets.top;
    int systemWindowInsetBottom = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom + insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom - systemBarsInsets.bottom;
    WindowInsetsCompat finalInsets = new WindowInsetsCompat.Builder().setInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.ime(), Insets.of(systemBarsInsets.left, systemWindowInsetTop, systemBarsInsets.right, Math.max(systemWindowInsetBottom - getBottomInset(), 0))).build();
    ViewCompat.onApplyWindowInsets(view, finalInsets);
    return insets;
}
Also used : WindowInsetsCompat(androidx.core.view.WindowInsetsCompat) Insets(androidx.core.graphics.Insets)

Example 3 with WindowInsetsCompat

use of androidx.core.view.WindowInsetsCompat in project FlexibleAdapter by davideas.

the class ViewPagerActivity method onCreate.

@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;
        }
    });
}
Also used : WindowInsetsCompat(androidx.core.view.WindowInsetsCompat) HeaderView(eu.davidea.samples.flexibleadapter.views.HeaderView) OnApplyWindowInsetsListener(androidx.core.view.OnApplyWindowInsetsListener) TabLayout(com.google.android.material.tabs.TabLayout) View(android.view.View) HeaderView(eu.davidea.samples.flexibleadapter.views.HeaderView) Toolbar(androidx.appcompat.widget.Toolbar)

Aggregations

WindowInsetsCompat (androidx.core.view.WindowInsetsCompat)3 View (android.view.View)2 OnApplyWindowInsetsListener (androidx.core.view.OnApplyWindowInsetsListener)2 ViewGroup (android.view.ViewGroup)1 Window (android.view.Window)1 Toolbar (androidx.appcompat.widget.Toolbar)1 CoordinatorLayout (androidx.coordinatorlayout.widget.CoordinatorLayout)1 Insets (androidx.core.graphics.Insets)1 AppBarLayout (com.google.android.material.appbar.AppBarLayout)1 CollapsingToolbarLayout (com.google.android.material.appbar.CollapsingToolbarLayout)1 TabLayout (com.google.android.material.tabs.TabLayout)1 HeaderView (eu.davidea.samples.flexibleadapter.views.HeaderView)1 WeakReference (java.lang.ref.WeakReference)1