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.
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.
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.
INVISIBLE
only hides the view.
GONE
however, will hide the view and prevent it from taking up any space.
toolbar.setVisibility(View.GONE);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With