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.

8
  • stackoverflow.com/questions/21504088/…
    – Fahim
    Mar 18, 2015 at 17:20
  • tried it already but it doesn't work. Do I have to put it inside onCreate() or the onCreateView() of the fragment?
    – smovie9
    Mar 18, 2015 at 17:27
  • OnActivityCreated or onCreateView
    – Fahim
    Mar 18, 2015 at 17:45
  • like this inside onCreate() or onCreateView(): ((ActionBarActivity)getActivity).getSupportActionBar().hide();
    – smovie9
    Mar 18, 2015 at 18:02
  • it does not work, has anyone other ideas?
    – smovie9
    Mar 18, 2015 at 19:51

10 Answers 10

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();
}
6
  • 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. Mar 28, 2018 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, 2018 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, 2020 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, 2020 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, 2020 at 5:41
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();
}
3
  • 9
    This won't show toolbar again when leaving that fragment.
    – Borja
    Jan 9, 2017 at 14:26
  • 1
    You need to show it: getYourActivity().getSupportActionBar().show();
    – ODAXY
    Nov 19, 2019 at 8: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, 2020 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);
    }
}
3
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.

1
  • Add some code to your post for a better answer quality.
    – Dhia
    Jan 8, 2016 at 16:52
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();
    }
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)
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()
        }
    }
2
  • Just make sure to call this in onPostCreate(savedInstanceState: Bundle?) and not onCreate or onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
    – Victor Ude
    Jan 9, 2021 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. Apr 9, 2021 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

1
  • getSupportActionBar() is not accessible from Fragment. You need to call getActivity() and cast if needed. Aug 10, 2020 at 19:25
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);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.