How to open Drawer Layout only with button?
Asked Answered
N

4

6

I'm working on application that has a tab structure, and use sliding movements to move through the tabs.

But now, I want to apply Drawer Layout. The problem is that the Drawer has slide to open events. How I can delete this event? My idea was that the Drawer only could open and close with a button. Is this possible? Thanks!

Nonbelligerent answered 9/8, 2013 at 7:28 Comment(1)
hello, did you manage to open the drawer by clicking on tab's button? please let me know thank you.Fructify
P
24

Just write

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

to prevent menu from listening to gesture

and use openDrawer and closeDrawer to change menu visibility

Philippine answered 13/8, 2013 at 2:56 Comment(2)
what would be the (int gravity arg) to open the drawerRadiocarbon
@AbdulWahab Gravity.LEFT or Gravity.RIGHT for RTL. Gravity.START/Gravity.END from API 14Gaur
L
16

By default the DrawerLayout is initially hidden from the view unless you put a code to open the Drawer, by the time there is a sliding event triggered.

From the Navigation Drawer example, the contain content_frame is used to dynamically display views inside the Drawer using fragments.

  <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

From the Fragment's onCreateView() you can include a button somewhere that has OnClickListener where in you put this code,

   //For me a better way in avoiding a `null pointer` in getting the DrawerLayout
   final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
   btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                            //Opens the Drawer
                drawer.openDrawer(Your View, Usually a ListView);
            }

                return false;
        });

You Can also use* to close the drawer.

drawer.closeDrawer(Your View, Usually a ListView);
Lots answered 13/8, 2013 at 2:37 Comment(2)
This worked for me. I did want to show the ActionBar. So I hide it, added a button to main layout and implemeneted openDrawer and closeDrawer methods. thanks @lordzdenFara
What do u mean by this listView in 'drawer.openDrawer(Your View, Usually a ListView);' @lordzdenHortatory
T
1

you can write this way

 mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            drawer.openDrawer(navigationView);

        }
    });
Template answered 6/11, 2016 at 11:15 Comment(0)
I
0

If you want to navigate between drawer item on click of button inside your fragments, you can use this

((YourMainActivity)getActivity()).selectItem(position);
Ileneileo answered 26/3, 2014 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.