51

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

How I can return ToolBar back programmatically?

Artem
  • 4,569
  • 12
  • 44
  • 86

3 Answers3

121

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."

starball
  • 20,030
  • 7
  • 43
  • 238
Jraco11
  • 4,526
  • 3
  • 20
  • 20
  • I have tried these, still not working. better solution is show and hide the CollapsingToolbar and set the AppBarLayout params dynamically. Do this when ever you show/hide the CollapsingToolbarLayout mAppBar.setLayoutParams(new CoordinatorLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); – Parikshit.S.Shekhawat Sep 16 '22 at 07:17
11

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: How to enable/disable toolbar scrolling programmatically when using design support library

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();
Community
  • 1
  • 1
johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • 1
    @johnarao07 hi man! Thanks for answer but it doesn't work for me. And this solution disable hiding/showing for toolbar. I want just show/hide whithout disabling – Artem Nov 27 '15 at 14:11
  • @ArtemShevchenko did you solve your problem? I am facing similar issue – user2095470 Dec 09 '15 at 06:03
4

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

Johnett Mathew
  • 307
  • 1
  • 10
  • 20