Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/Show Toolbar programmatically on CoordinatorLayout

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

How I can return ToolBar back programmatically?

like image 295
Artem Avatar asked Nov 27 '15 13:11

Artem


People also ask

How do I use co-coordinatorlayout?

CoordinatorLayout works by searching through any child view that has a CoordinatorLayout Behavior defined either statically as XML with a app:layout_behavior tag or programmatically with the View class annotated with the @DefaultBehavior decorator.

How to hide action bar title bar toolbar on scroll down Android Studio?

So here is the complete step by step tutorial for Hide Action Bar Title Bar Toolbar on Scroll Down Android Studio example tutorial. 1. Open your project’s build.gradle ( Module : app ) file. 2. Please add below code inside your build.gradle ( Module : app ) file. 3. Screenshot of build.gradle ( Module : app ) file after adding above code.

Can appbarlayout be nested within a coordinatorlayout?

Note: AppBarLayout currently expects to be the direct child nested within a CoordinatorLayout according to the official Google docs. Next, we need to define an association between the AppBarLayout and the View that will be scrolled. Add an app:layout_behavior to a RecyclerView or any other View capable of nested scrolling such as NestedScrollView.

How to hide and show Toolbar while scrolling in recyclerview?

Add a app:layout_behavior to a RecyclerView or any other View prepared of nested scrolling such as NestedScrollView. By Adding the above property in your XML, you can achieve the ability to hide and showing toolbar while scrolling.


3 Answers

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!

like image 140
Jraco11 Avatar answered Oct 03 '22 13:10

Jraco11


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();
like image 42
johnrao07 Avatar answered Oct 03 '22 15:10

johnrao07


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

like image 4
Johnett Mathew Avatar answered Oct 03 '22 15:10

Johnett Mathew