android hide toolbar in specific fragment

48,643

Solution 1

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();
}

Solution 2

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();
}

Solution 3

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);
    }
}

Solution 4

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.

Solution 5

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();
    }
Share:
48,643
smovie9
Author by

smovie9

Updated on November 23, 2021

Comments

  • smovie9
    smovie9 over 2 years

    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.