Hide/Show Toolbar programmatically on CoordinatorLayout

AndroidAndroid ToolbarAndroid Recyclerview

Android Problem Overview


When I scroll my RecycleView ToolBar hide or show (with animation). enter image description here

How I can return ToolBar back programmatically?

Android Solutions


Solution 1 - Android

If your toolbar is inside an AppBarLayout which is probably inside your CoordinatorLayout then something like this should work.

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
            appBarLayout.setExpanded(true, true);

Or to collapse it

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
            appBarLayout.setExpanded(false, true);

Here is the definition

setExpanded(boolean expanded, boolean animate)

Take note that this method is available from v23 of the support library, here is some documentation for reference, the key thing to note is "As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout." Hope this helps!

Solution 2 - Android

Is that what you looking for?

Toolbar toolbar = findViewById(R.id.toolbar);  // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);  // clear all scroll flags

link: https://stackoverflow.com/questions/33127720/how-to-enable-disable-toolbar-scrolling-programmatically-when-using-design-suppo

In order to hide the Toolbar your can just do something like this:

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

If you want to show it again you call:

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

Solution 3 - Android

My problem was very similar to @Artem I tried many fix but none of them worked for me. @Jraco11's answer is correct when you use AppBarLayout. @johnrao07 not worked for me. But I found a perfect solution for this problem when we use Toolbar.

To hide Toolbar programatically

if (toolbar.getParent() instanceof AppBarLayout){
                    ((AppBarLayout)toolbar.getParent()).setExpanded(false,true);
                }

To show Toolbar programatically

if (toolbar.getParent() instanceof AppBarLayout){
                        ((AppBarLayout)toolbar.getParent()).setExpanded(true,true);

Refer original answer(answer by @Android HHT):- programmatically-show-toolbar-after-hidden-by-scrolling-android-design-library

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArtemView Question on Stackoverflow
Solution 1 - AndroidJraco11View Answer on Stackoverflow
Solution 2 - Androidjohnrao07View Answer on Stackoverflow
Solution 3 - AndroidJohnett MathewView Answer on Stackoverflow