43

I have a navigation bar without any actionbar (I don't want an actionbar). I'm trying to make it so that I have a button that can open the navigation drawer.

I know there's a method called openDrawer for the DrawerLayout http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html#openDrawer(android.view.View)

I didn't know how to use it, but i have tried making a button when click, runs this method:

DrawerLayout mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(mDrawerLayout);

When i click it on it, it gives me a Java NullPointerException. Anybody has any idea?

EDIT: This code is inside a fragment, and I'm trying to refer those drawer layout outside the fragment. I used debugger, and it is showing that mDrawlerLayout is NULL.

Any advice?

Thanks!

CynthiaDDurazo
  • 503
  • 1
  • 4
  • 11

7 Answers7

56

It's giving you a null pointer because you are trying to find the drawer layout in the fragment's view, when it is actually in the activities view.

A quick hack to do what you want is to find the view like:

getActivity().findViewById(R.id.drawer_layout)

That should work. A better way is to have a method on the activity for opening the drawer

public void openDrawer(){
    mDrawerLayout.openDrawer(mDrawerLayout);
}

In the activity onCreate run your findViewById:

mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

mDrawerLayout should be a member variable of your activity.

Then in your fragment you can call:

//cast activity to MyActivity so compiler doesn't complain
((MyActivity)getActivity()).openDrawer();

An even better way to do it is to create a listener in the fragment and set the activity as a listener to the fragment. Then you can call a method on the activity, similar to above. I'll let you do some research on how to do that.

slezadav
  • 6,104
  • 7
  • 40
  • 61
athor
  • 6,848
  • 2
  • 34
  • 37
  • 15
    I had to use `mDrawerLayout.openDrawer(Gravity.LEFT);` in `openDrawer` method to make it work. (I am using navigation drawer without both fragments & action bar) Otherwise, it just hangs the app. – Ashish Tanna Oct 13 '14 at 20:06
  • Is it possible to remove the default icon? In that position I want to use a different option menu. – JCarlosR Nov 11 '16 at 01:03
40
drawerLayout.openDrawer(Gravity.START);
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Aliya
  • 1,168
  • 12
  • 17
4

It works on Button click

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

            drawer.openDrawer(GravityCompat.START);

        }
    });
2

Thanks to @athor & @Ashish Tana.

It took me so much time to figure out the error (NullPointerException) I am getting.

Mine works this way; Instead of getView(), I use getActivity() and open the drawer by mDrawerLayout.openDrawer(Gravity.LEFT);.

mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
final ImageButton btnOpenDrawer = (ImageButton) getView().findViewById(R.id.drawer_menu);

        btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });
Jei
  • 317
  • 5
  • 15
  • this really work I place this onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //place the code here } – Faisal Jun 07 '17 at 12:28
2

I have a simpler solution which uses isDrawerOpen() of DrawerLayout.

The code below closes or opens the navigation drawer based on the drawer's current state (Opened/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 navigation drawer is not open yet, open it else close it.
        if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
        else navDrawer.closeDrawer(Gravity.END);
    }
});
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
  • For close the drawer I had to use `navDrawer.closeDrawer(Gravity.START);` to make it work. Otherwise, I just get an _IllegalArgumentException_ because "No drawer view found with gravity RIGHT". – Denis Francia Karell Dec 28 '22 at 02:40
0

I wrote an answer about this here: https://stackoverflow.com/a/18199771/880349

   //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 directly get the current drawer inside a view/fragement so that you won't get a NullPointerException

Community
  • 1
  • 1
rahstame
  • 2,148
  • 4
  • 23
  • 53
0

Kotlin Solution
If you want to open it from a fragment, you can use:

activity?.drawerLayout?.openDrawer(GravityCompat.START)

or inside the activity directly:

drawerLayout.openDrawer(GravityCompat.START)

Note:
If you are not familiar with the '?', I highly recommend reading about null saftey from kotlin's documentation (It is very short don't worry): https://kotlinlang.org/docs/reference/null-safety.html

Ahmed Ashour
  • 562
  • 5
  • 14