Search code examples
android-studioandroid-toolbarandroid-appbarlayoutappbarandroid-collapsingtoolbarlayout

Prevent collapsingToolbar from expanding for certain fragments


I'm trying to prevent a CollapsingToolbar from expanding for one of my fragments.

Currently, when I use the method setExpanded(), I am able to see the toolbar in its collapsed state.

appBarLayout.setExpanded(false, true);

However, the collapsingToolbar is still expandable, which is not what I want. I want the collapsingToolbar to not be expandable for this particular fragment.

In other words, I want the collapsingToolbar to behave and look like a regular toolbar (i.e. "not expandable") for this particular fragment only.

I am using Mike Penz's Material Drawer. The code below shows the relevant code, with notes indicating what I have tried.

private void buildDrawer(){
    Drawer drawer = new DrawerBuilder()
            .withActivity(this)
            .withFullscreen(true)
            .withTranslucentStatusBar(true)
            .withToolbar(toolbar)
            .withAccountHeader(accountHeader)
            .addDrawerItems(
                item1,
                item2,
                new DividerDrawerItem(),
                item3,
                item4
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    Fragment f = new Fragment();
                    switch (position) {
                        //other cases not shown                            
                        case 2:
                            f = new LocateFragment();
                            appBarLayout.setExpanded(false, true);
                            //Adding the following three lines don't work - causes the toolbar to be unscrollable, but in its expanded form
                            //AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams)collapsingToolbarLayout.getLayoutParams();
                            //p.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); 
                            //collapsingToolbarLayout.setLayoutParams(p);
                            //toolbar.setCollapsible(false); doesn't work either
                            collapsingToolbarLayout.setTitle("Locate Events");

                            setInvisibleAddPhotoFab();
                            break;
                        //other cases not shown 
                    }
                    fragmentTransaction.replace(R.id.frame_fragments, f);
                    fragmentTransaction.commit();
                    return false; //close drawer onclick
                }
            })
            .build();

    loadBackdrop();
}

This is what I want - I want the toolbar to be unexpandable:

enter image description here

Currently, however, it is still expandable, so the image below is NOT what I want:

enter image description here

UPDATE: I was able to collapse the toolbar and prevent it from expanding like so (code below), but I've run into another issue - the title no longer appears on the collapsingToolbar when I set it like so: collapsingToolbarLayout.setTitle("string");

appBarLayout.setExpanded(false, true);
int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
lp.height = px;
appBarLayout.setLayoutParams(lp);

Solution

  • I was able to resolve this problem using the following (as shown here by @DanielPersson):

    collapsingToolbarLayout.setTitleEnabled(false);  
    toolbar.setTitle(title);