@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setHasOptionsMenu(true); mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appbar); mCollapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar); mCollapsingToolbarLayout.setTitle(getString(R.string.app_name)); mToolbar = (Toolbar) view.findViewById(R.id.toolbar); ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); StatusBarCompat.setStatusBarColorForCollapsingToolbar(getActivity(), mAppBarLayout, mCollapsingToolbarLayout, mToolbar, Color.YELLOW); }
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 { collapsingToolbarLayout.setFitsSystemWindows(false); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false); collapsingToolbarLayout.setStatusBarScrimColor(statusColor);
private void dispatchDrawInternal(@NonNull Canvas canvas) { Collections.sort(getViews(), new ElevationComparator()); super.dispatchDraw(canvas); if (stroke != null) drawStroke(canvas); if (rippleDrawable != null && rippleDrawable.getStyle() == RippleDrawable.Style.Over) rippleDrawable.draw(canvas); if (insetColor != 0) { paint.setColor(insetColor); paint.setAlpha(255); if (insetLeft != 0) canvas.drawRect(0, 0, insetLeft, getHeight(), paint); if (insetTop != 0) canvas.drawRect(0, 0, getWidth(), insetTop, paint); if (insetRight != 0) canvas.drawRect(getWidth() - insetRight, 0, getWidth(), getHeight(), paint); if (insetBottom != 0) canvas.drawRect(0, getHeight() - insetBottom, getWidth(), getHeight(), paint); } }
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); params.height = -1; toolbar.setTitleEnabled(false); setTitle(title); toolbar.setTitleEnabled(true); toolbar.setTitle(title); toolbar.setTitleEnabled(false); setTitle(title); params.setScrollFlags(0);
/** * Set layout scroll flags for the collapsing toolbar layout. * <p>Useful to change the collapse mode dynamically. * * @param flags The scroll flags for the collapsing toolbar layout. */ public void setCollapsingToolbarLayoutFlags(@AppBarLayout.LayoutParams.ScrollFlags int flags) { if (mCollapsingToolbarLayout != null) { AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mCollapsingToolbarLayout.getLayoutParams(); params.setScrollFlags(flags); mCollapsingToolbarLayout.setLayoutParams(params); } }
@Override public void setupToolbarImage(Bitmap bm) { ImageView collapsingImage = findViewById(R.id.collapsing_image); if (collapsingImage != null) { collapsingImage.setImageBitmap(bm); // FIXME DIRTY HACK: Manually fix the toolbar size to the screen width CollapsingToolbarLayout toolbar = findViewById(R.id.collapsing_toolbar); AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); params.height = getWindow().getDecorView().getMeasuredWidth(); // Always expand the toolbar to show the complete image AppBarLayout appbar = findViewById(R.id.appbar); appbar.setExpanded(true, false); } }
/** * Add a backdrop view for the collapsing toolbar layout which will be shown when it is * expanded and will be hidden on collapsing the toolbar. * * @param view The view to be added as the backdrop frame. * @param expandedTitleColor The title color when the toolbar is expanded. */ public void setAppBarBackDrop(@Nullable View view, @ColorInt int expandedTitleColor) { if (DynamicTheme.getInstance().get().isBackgroundAware()) { expandedTitleColor = DynamicColorUtils.getContrastColor( expandedTitleColor, DynamicTheme.getInstance().get().getPrimaryColor()); } if (mCollapsingToolbarLayout != null) { if (mFrameBackDrop.getChildCount() > 0) { mFrameBackDrop.removeAllViews(); } if (view != null) { mFrameBackDrop.addView(view); setAppBarTransparent(true); mCollapsingToolbarLayout.setExpandedTitleColor(expandedTitleColor); mCollapsingToolbarLayout.setCollapsedTitleTextColor(expandedTitleColor); } } }
@Override public void setStatusBarColor(@ColorInt int color) { super.setStatusBarColor(color); if (!isDrawerActivity()) { if (mCoordinatorLayout != null) { mCoordinatorLayout.setStatusBarBackgroundColor(getStatusBarColor()); } if (mCollapsingToolbarLayout != null) { mCollapsingToolbarLayout.setStatusBarScrimColor(getStatusBarColor()); mCollapsingToolbarLayout.setContentScrimColor( DynamicTheme.getInstance().get().getPrimaryColor()); } else { setWindowStatusBarColor(getStatusBarColor()); } } }
public void setHeaderTextColor(@ColorInt int color) { collapsingToolbar.setCollapsedTitleTextColor(color); slogan.setTextColor(color); version.setTextColor(color); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (mCustomContentViewId != 0) setContentView(mCustomContentViewId); else setContentView(R.layout.activity_setup_big_header); AppBarLayout appBar = findViewById(R.id.appbar); CollapsingToolbarLayout toolbarLayout = findViewById(R.id.toolbar_layout); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); mLayout = findViewById(R.id.layout); mLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> { int height = mLayout.getHeight(); CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams(); params.height = height / 3; appBar.setLayoutParams(params); int childHeight = (mContentView != null ? mContentView.getHeight() : 0); if (mContentView instanceof NestedScrollView) childHeight = ((NestedScrollView) mContentView).getChildAt(0).getHeight(); boolean needsScroll = (mContentView != null && childHeight > height - params.height); AppBarLayout.LayoutParams paramsToolbar = (AppBarLayout.LayoutParams) toolbarLayout.getLayoutParams(); paramsToolbar.setScrollFlags(needsScroll ? (AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) : 0); toolbarLayout.setLayoutParams(paramsToolbar); }); }
collapsingToolbarLayout.setTitle(getString(R.string.drawer_item_collapsing_toolbar_drawer));
@Override public void setTitle(@NonNull CharSequence title) { collapsingToolbar.setTitle(title); }
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (scrollRange == -1) { scrollRange = appBarLayout.getTotalScrollRange(); } // verify if the toolbar is completely collapsed and set the movie name as the title if (scrollRange + verticalOffset == 0) { mBinding.collapsingToolbar.setTitle( mViewModel.getResult().getValue().data.movie.getTitle()); isShow = true; } else if (isShow) { // display an empty string when toolbar is expanded mBinding.collapsingToolbar.setTitle(" "); isShow = false; } } });
@Override public void setTitle(@Nullable CharSequence title) { super.setTitle(title); if (getSupportActionBar() != null) { getSupportActionBar().setTitle(title); } if (mCollapsingToolbarLayout != null) { mCollapsingToolbarLayout.setTitle(title); } }