Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide android.support.v7.widget.Toolbar programmatically

In my activity I do:

setSupportActionBar(toolbar);

where toolbar is an instance of android.support.v7.widget.Toolbar

Is there any way after this to hide and show back Toolbar widget programmatically? I already tried

toolbar.setVisibility(View.INVISIBLE);

but this only makes it invisible and it still takes space, so that content of the activity starts after it, and I see white space instead in the header.

like image 441
afrish Avatar asked Jan 10 '15 21:01

afrish


People also ask

How to remove Toolbar in Android programmatically?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.

What is Androidx Appcompat widget toolbar?

androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.


2 Answers

INVISIBLE only hides the view.

GONE however, will hide the view and prevent it from taking up any space.

toolbar.setVisibility(View.GONE);
like image 106
Simas Avatar answered Oct 05 '22 02:10

Simas


Here is my code try this. It worked perfectly for me.

    private static final float APPBAR_ELEVATION = 14f;

    private void hideAppBar(final AppBarLayout appBar) {
                appBar.animate().translationY(-appBar.getHeight()).setInterpolator(new LinearInterpolator()).setDuration(500);

    }
    public void showAppBar(final AppBarLayout appBar){
                appBar.animate().translationY(0).setInterpolator(new LinearInterpolator()).setDuration(500).setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                appBar.setElevation(APPBAR_ELEVATION);
                            }
                });
    }

Hope this might help you

like image 36
Anirudh R.Huilgol. Avatar answered Oct 05 '22 02:10

Anirudh R.Huilgol.