android hide toolbar in specific fragment

smovie9 picture smovie9 · Mar 18, 2015 · Viewed 39.8k times · Source

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.

Answer

Soni Kumar picture Soni Kumar · Jul 5, 2016

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