Hide/Show Toolbar programmatically on CoordinatorLayout

Artem picture Artem · Nov 27, 2015 · Viewed 28.9k times · Source

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

How I can return ToolBar back programmatically?

Answer

Jraco11 picture Jraco11 · Dec 10, 2015

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!