17

I am currently learning about the Navigation Drawer from the android site, and I am using their example http://developer.android.com/training/implementing-navigation/nav-drawer.html

What I want is to add a button in the MainActivity which would be able to open the NavigationDrawer. I need to do it programmatically, not in XML. How can I do that?

Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81

2 Answers2

38

Create a method in MainActivity which contains your drawerLayout.

public void open()
{
    mDrawerLayout.openDrawer(Gravity.LEFT);
}


and from Your fragment In oncreateView() method As you want new Button Programmatically add Button in Your Root inflated layout. Your fragment has button
bellow I modified fragment try

 public static class PlanetFragment extends Fragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";

    public PlanetFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
        int i = getArguments().getInt(ARG_PLANET_NUMBER);
        String planet = getResources().getStringArray(R.array.planets_array)[i];

        int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(planet);
        RelativeLayout root=(RelativeLayout)rootView.findViewById(R.id.root);
        Button button=new Button(getActivity());            
        LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        button.setText("openDrawer");
        root.addView(button);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ((MainActivity)getActivity()).open();
            }
        });
        return rootView;
    }
    }
 }


You can try this code in your fragment..

Pravin
  • 1,362
  • 12
  • 21
  • but how do I add the button? – Sartheris Stormhammer Jul 25 '13 at 08:52
  • In answer I have not added button in activity but in fragment,So you can access Drawer from any fragment and from any views click event by calling activity open() method. – Pravin Jul 25 '13 at 09:41
  • Why Gravity.LEFT? Why not RIGHT or something else? Is that really relevant or do you just need an int? Not questioning you -- hoping for better understanding. – Bill Mote Sep 09 '14 at 19:12
  • usually navigation drawer open from the left side(as by clicking the icon left to the app logo). See the standered Google apps. But it is also possible to open Nav. drawer from right too..(depends upon needs) – Pravin Sep 10 '14 at 06:38
  • HI want to open drawer from any activity of app. – PriyankaChauhan Jun 12 '17 at 11:20
  • @PriyankaChauhan I 'll suggest you to look into [same-navigation-drawer-in-different-activities](https://stackoverflow.com/questions/19451715/same-navigation-drawer-in-different-activities) and [android-navigationdrawer-multiple-activities-same-menu](https://stackoverflow.com/questions/36095691/android-navigationdrawer-multiple-activities-same-menu) and [navigation-drawer-to-switch-activities-instead-of-fragments](https://stackoverflow.com/questions/19442378/navigation-drawer-to-switch-activities-instead-of-fragments) – Pravin Jun 19 '17 at 11:15
13

Create your Button in onCreate(Bundle) method:

Button button = new Button(this);

Find your DrawerLayout:

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

Set an OnClickListener on this button:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        mDrawerLayout.openDrawer(Gravity.LEFT);   
    }
)

This will give you an empty drawer. If you have a View that you would like to place inside the drawer, replace:

mDrawerLayout.openDrawer(Gravity.LEFT);

with:

mDrawerLayout.openDrawer(myCustomView);

If you want the button to toggle the drawer(close the drawer if its open or, open it if its closed) use the following OnClickListener:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            mDrawerLayout.openDrawer(Gravity.LEFT); 
        }  
    }
)

If you are using a custom view, use this OnClickListener:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        if (mDrawerLayout.isDrawerOpen(myCustomView)) {
            mDrawerLayout.closeDrawer(myCustomView);
        } else {
            mDrawerLayout.openDrawer(myCustomView); 
        }  
    }
)
Vikram
  • 51,313
  • 11
  • 93
  • 122