64

I have a problem that I don't know how to solve. How do you hide a toolbar in a specific fragment, I have already been searching around on the internet and what I found was communicating activity and fragment would solve it. But it doesn't work for me at all, here is my code:

main_activity:

public class MainActivity extends ActionBarActivity implements like_frag.OnHideToolbar{

....

public void onHidingToolbar(int position){
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        if(toolbar == null){
            getSupportActionBar().hide();
        }else{
            getSupportActionBar().hide();
        }
    }

like_frag.java

public class like_frag extends Fragment {

    OnHideToolbar mCallback;
    Toolbar toolbar;

    public interface OnHideToolbar {
        public void onHidingToolbar(int position);
    }

    public void onAttach(Activity activity){

        try{
            mCallback = (OnHideToolbar) activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString() + "error implementing");
        }
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.swipefrag, container, false);

        toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
}

thanks in advance.

I have a drawer inside the toolbar.

smovie9
  • 788
  • 1
  • 8
  • 16

10 Answers10

128

Put this code in fragment in which you want to hide toolbar...

 @Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
Soni Kumar
  • 1,533
  • 1
  • 11
  • 12
  • It worked perfect. I tried many answers in stackoverflow. But nothing worked. You saved my day. Thanks a lot. I also agree, this should be the correct answer. – Dinith Rukshan Kumara Mar 28 '18 at 10:01
  • This is awesome. Kept trying to call `setVisibility ()` on it from the fragment. `.hide()` and `.show()` work perfectly from within the fragment. – Mr.Drew Sep 11 '18 at 15:44
  • 7
    Beware of the flickering issue while using this! I lost my sleep for 2 days profiling my constraint layouts and bottom nav, checking for performance issues. – nandu May 20 '20 at 18:50
  • @nandu How did you solve it? Currently facing the same issues with a constraint layout and bottom nav, it keeps getting stretched up before resetting, which looks horrible. – Big_Chair Sep 25 '20 at 22:11
  • 2
    @Big_Chair I think I solved it by removing it altogether and setting it where needed using (activity as AppCompatActivity?)?.setSupportActionBar(appbar) – nandu Sep 27 '20 at 05:41
  • For the sake of consistency, shouldn't we show the toolbar in `onPause()`? Or hide it in `onStart()`? – Erik Medina Mar 24 '22 at 14:17
44

In the fragment's onCreate method call:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
Replace AppCompateActivity with the activity class you used.

Edited:

You could simply use the onResume method to call hide() and the onStop method to call show() as suggested in some of the comments.

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}

@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
xdzc
  • 1,421
  • 1
  • 17
  • 23
  • 9
    This won't show toolbar again when leaving that fragment. – Borja Jan 09 '17 at 14:26
  • 1
    You need to show it: getYourActivity().getSupportActionBar().show(); – ODAXY Nov 19 '19 at 08:42
  • 1
    As @ODAXY said , you need to call getYourActivity().getSupportActionBar().show(); on the fragment's onDestroy() method. So that rest of the application behaviour is rolled back. – Xenon Kfr Jan 15 '20 at 13:08
28

If you are using the new Navigation Component, add this while setting up the toolbar

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
   @Override
   public void onDestinationChanged(@NonNull NavController controller,
           @NonNull NavDestination destination, @Nullable Bundle arguments) {
       if(destination.getId() == R.id.full_screen_destination) {
           toolbar.setVisibility(View.GONE);
           bottomNavigationView.setVisibility(View.GONE);
       } else {
           toolbar.setVisibility(View.VISIBLE);
           bottomNavigationView.setVisibility(View.VISIBLE);
       }
   }
});

And for Kotlin, you can do the following:

navController.addOnDestinationChangedListener { _, destination, _ ->
    if(destination.getId() == R.id.full_screen_destination) {
        toolbar.setVisibility(View.GONE)
        bottomNavigationView.setVisibility(View.GONE);
    } else {
        toolbar.setVisibility(View.VISIBLE)
        bottomNavigationView.setVisibility(View.VISIBLE);
    }
}
MiStr
  • 1,193
  • 10
  • 17
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
  • 2
    Here is the Android [official doc](https://developer.android.com/guide/navigation/navigation-ui#listen_for_navigation_events) explaining `addOnDestinationChangedListener` – Sanjeev Jan 23 '20 at 10:16
  • 1
    It is also worth noting for beginners to make sure you [remove the action bar before adding the toolbar](https://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor) – Rubek Joshi Apr 03 '20 at 20:31
  • 1
    Hey. This is really a good solution. Because I need to hide the action bar and nottom navigation view on some of the child fragment – Andrea Liureta Nov 29 '21 at 16:37
9

Create an interface in the fragment and use it to tell the parent activity to hide the toolbar.

Add these lines to your fragment:

private OnEventListener listener;

public interface OnEventListener {

    void hideToolbar() ;
}

public void setOnEventListener(OnEventListener listener) {

    this.listener = listener;
}

After creating your fragment in the main activity add:

    myFragment.setOnEventListener(new MyFragment.OnEventListener() {
        @Override
        public void hideToolbar() {

            getSupportActionBar().hide();
        }
    });

Whenever you need to hide the toolbar execute:

listener.hideToolbar();

from inside your fragment.

Chris Read
  • 307
  • 4
  • 11
8

Just add these methods to the fragment where you want to diable the toolbar ,and also in the fragment's onStop() make it visible again.

 @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
    }

    @Override
    public void onStop() {
        super.onStop();
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();
    }
Kaustubh Bhagwat
  • 2,773
  • 1
  • 14
  • 20
6

in kotlin hide and show supportActionBar as follows:

override fun onResume() {
    super.onResume()
    (activity as AppCompatActivity).supportActionBar?.hide()
}

override fun onStop() {
    super.onStop()
    (activity as AppCompatActivity).supportActionBar?.show()
}

and if you want to have your own custom toolbar, in OncreateView set:

//your Custom toolbar in xml
val toolbar = binding.toolbar
(activity as AppCompatActivity).setSupportActionBar(toolbar)
cs95
  • 379,657
  • 97
  • 704
  • 746
Sanam Yavarpor
  • 348
  • 3
  • 10
5

Simply use supportActionBar?.hide() or supportActionBar?.show(). If you are using NavigationController:

 navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.loginSuccessFragment) {
            supportActionBar?.hide()
        } else {
            supportActionBar?.show()
        }
    }
avisper
  • 878
  • 12
  • 11
  • Just make sure to call this in `onPostCreate(savedInstanceState: Bundle?)` and not `onCreate` or `onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)` – Victor Ude Jan 09 '21 at 18:49
  • This hides the action bar, but when going back to other screens, the layouts are now positioned behind the action bar, shifted up. – Gary Frewin Apr 09 '21 at 20:48
3

Put this code in fragment in which you want to hide toolbar...

Add this( ((AppCompatActivity)getActivity()).getSupportActionBar().hide();) in onCreateView or in onResume.

and do this in onDestroy()

@Override
public void onDestroy() {
super.onDestroy();
((AppCompatActivity)getActivity()).getSupportActionBar().show();}
2

use getSupportActionBar().hide(); and getSupportActionBar().show(); in lifeCycle methods

0

You can try it.

 @Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
    if (destination.getId() == R.id.nav_dashboard){
        if (toolbar !=null){
           toolbar.setVisibility(View.GONE);
        }
    }else {
        toolbar.setVisibility(View.VISIBLE);
    }
}
Kumar Santanu
  • 603
  • 1
  • 7
  • 14