How to open navigation drawer on button click in main fragment?
Asked Answered
D

6

27

I have made an app with one activity which uses a navigation drawer to open a number of different fragments. I have the actionbar drawertoggle, but it is not very visible. If I place a button in the onCreateView in my main fragment(the fragment that appears when my app first starts up), how can I get it to open the navigation drawer controlled by my activity?


This seems to work. The answer is much simpler than I thought it would be.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragView = inflater.inflate(R.layout.mainmenu, container, false);
    button1 = (Button) fragView.findViewById(R.id.button1);         
    mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)getActivity().findViewById(R.id.left_drawer);
    button1.setOnClickListener(this);
    return fragView;
}


@Override
public void onClick(View v) {       

        mDrawerLayout.openDrawer(mDrawerList);          

    }

Thank you for your answers.

Demonstration answered 18/10, 2013 at 6:16 Comment(0)
M
77

if you need open the slide:

mDrawerLayout.openDrawer(Gravity.LEFT); //Edit Gravity.START need API 14

if you need close the slide

mDrawerLayout.closeDrawer(Gravity.LEFT); //Edit Gravity.START need API 14

EXAMPLE

my mDrawerLayout is instanced here:

mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout);

my slide state:

mSlideState=false;

if you need to know the slide menu state (closed, opened). Use this code:

mDrawerLayout.setDrawerListener(new ActionBarDrawerToggle(this, 
    mDrawerLayout, 
    R.drawable.ic_menu_slide,
    0,
    0){
@Override
public void onDrawerClosed(View drawerView) {                       
    super.onDrawerClosed(drawerView);
    mSlideState=false;//is Closed
}
@Override
public void onDrawerOpened(View drawerView) {                       
    super.onDrawerOpened(drawerView);
    mSlideState=true;//is Opened
}});

finally. You can use your click event like this:

public void clickEventSlide(){
if(mSlideState){                
    mDrawerLayout.closeDrawer(Gravity.END);
}else{              
    mDrawerLayout.openDrawer(Gravity.END);
}}

In my case, my slide menu is at the right (Gravity.END), but if you need it on the left, try with Gravity.START

Mesozoic answered 30/1, 2015 at 17:23 Comment(4)
you might consider mDrawerLayout.isDrawerOpen(drawerView) instead of flagToilsome
i want this layout above my view. IN THIS THE LAYOUT IS MOVING TO A SIDE. what should i do?Eisenach
in your xml, last (bottom-most) declared view will have highest z-index (top) by default, above answer should be accepted if it resolved the issue.Flatfooted
Everything works fine, but setDrawerListener is deprecated must use addDrawerListenerNovelize
G
21

You Should Use isDrawerOpen()

The piece of code below automatically closes or opens the navigation drawer based on the drawer's current state (Opened or Closed)

Button hamMenu = findViewById(R.id.ham_menu);

hamMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
        // If the navigation drawer is not open then open it, if its already open then close it.
        if(!navDrawer.isDrawerOpen(Gravity.START)) navDrawer.openDrawer(Gravity.START);
        else navDrawer.closeDrawer(Gravity.END);
    }
});
Grating answered 28/10, 2018 at 12:36 Comment(2)
You are opening the left drawer and closing the right one? Otherwise why you use gravity.start in first line and gravity.end in the second? also why do you use both GravityCompat and Gravity? Just to know if I missed something, because in the documentation gravity is used to identify the drawer.Easterly
@A.Harkous I am not opening the left drawer and closing the right one. I am using Gravity.START and Gravity.END for the purposes that when the same button is clicked, if the drawer is not open it'll be opened, otherwise if the drawer was already open it will closed. This servers the purpose of utilizing the same button to either close or open the Navigation Drawer based on it's current state. And yesh, using GravityCompat was a typo. Fixed ItGrating
M
4

if you are using from default navigation activity in android you just have to add this code in click listener of button --->

mDrawerLayout.openDrawer(Gravity.START);

for closing you do not have to do something.

Mcdermott answered 15/3, 2018 at 15:36 Comment(1)
This causes the hamburger menu to disappear and be replaced with the Up arrow.Phiz
C
4

Use these lines to open and close the drawer on a certain event:

Code snippet for opening drawer:

drawerLayout.openDrawer(Gravity.START);

Code snippet for closing drawer:

drawerLayout.closeDrawer(Gravity.LEFT);

openDrawer(gravity_of_navigation_view_to_be_shown)

in openDrawer("gravity"), in "gravity" section, you have to input the gravity of the Navigation View like given above:

Gravity.LEFT
Gravity.RIGHT
Gravity.START
Gravity.END

I think thats the best answer.

Core answered 22/9, 2019 at 18:7 Comment(0)
U
3

To apply the toolbar as the app bar, first make sure your activity extends from AppCompatActivity. Then call setSupportActionBar() and pass the Toolbar object from your layout:

    toolbar=(Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);

    drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }
    else{
        super.onBackPressed();
    }
}

}

Unsnarl answered 11/4, 2019 at 12:5 Comment(0)
L
0

The simplest way in my opinion

   @Override
    public void onBackPressed() {
        if(mDrawerLayout.isDrawerOpen(findViewById(R.id.navigationViewId))){
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        }else
        super.onBackPressed();
    }
Lemniscus answered 3/10, 2022 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.