android hide toolbar in specific fragment
Asked Answered
V

10

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.

Verecund answered 18/3, 2015 at 16:58 Comment(8)
#21504588Filippa
tried it already but it doesn't work. Do I have to put it inside onCreate() or the onCreateView() of the fragment?Verecund
OnActivityCreated or onCreateViewFilippa
like this inside onCreate() or onCreateView(): ((ActionBarActivity)getActivity).getSupportActionBar().hide();Verecund
it does not work, has anyone other ideas?Verecund
If you're using the AppCompat library, are you calling setSupportActionBar(Toolbar) in your Activity's onCreate() method?Nickinickie
Yes I called setSupportActionBar(toolbar)Verecund
Is there a problem when I call setsupportactionbar(toolbar);?Verecund
K
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();
}
Kiddush answered 5/7, 2016 at 6:11 Comment(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.Escobedo
This is awesome. Kept trying to call setVisibility () on it from the fragment. .hide() and .show() work perfectly from within the fragment.Espalier
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.Robbins
@Robbins 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.Dougall
@Dougall I think I solved it by removing it altogether and setting it where needed using (activity as AppCompatActivity?)?.setSupportActionBar(appbar)Robbins
For the sake of consistency, shouldn't we show the toolbar in onPause()? Or hide it in onStart()?Trunnion
W
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();
}
Windshield answered 15/7, 2015 at 19:3 Comment(3)
This won't show toolbar again when leaving that fragment.Perspicacity
You need to show it: getYourActivity().getSupportActionBar().show();Melodist
As @Melodist said , you need to call getYourActivity().getSupportActionBar().show(); on the fragment's onDestroy() method. So that rest of the application behaviour is rolled back.Pleurodynia
N
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);
    }
}
Nonagon answered 20/9, 2019 at 11:27 Comment(3)
Here is the Android official doc explaining addOnDestinationChangedListenerNonagon
It is also worth noting for beginners to make sure you remove the action bar before adding the toolbarOrphanage
Hey. This is really a good solution. Because I need to hide the action bar and nottom navigation view on some of the child fragmentDobby
B
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.

Bertero answered 8/1, 2016 at 16:29 Comment(1)
Add some code to your post for a better answer quality.Orland
P
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();
    }
Pohai answered 28/7, 2017 at 8:49 Comment(0)
B
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)
Bartolemo answered 23/6, 2019 at 5:32 Comment(0)
V
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()
        }
    }
Venola answered 23/12, 2020 at 21:48 Comment(2)
Just make sure to call this in onPostCreate(savedInstanceState: Bundle?) and not onCreate or onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)Bonitabonito
This hides the action bar, but when going back to other screens, the layouts are now positioned behind the action bar, shifted up.Salome
M
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();}
Maurita answered 6/3, 2018 at 9:56 Comment(0)
Q
2

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

Quotation answered 10/8, 2020 at 17:44 Comment(1)
getSupportActionBar() is not accessible from Fragment. You need to call getActivity() and cast if needed.Pryor
I
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);
    }
}
Ic answered 22/11, 2021 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.