Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - open navigation drawer by clicking the app icon?

I use a navigation drawer in my app (the type of navigation that you open by sliding from the left side of the screen). Now, usually you can also open it by tapping the app icon, and when I looked it up, I found a whole bunch of code to add to your activity. And just for a simple button. I suppose thats not exactly what I am looking for? And if it really needs to be so much code for a single button, what is the best way to make the code more clear? Thank you, and sorry for being such an android newb.

like image 423
Martin Avatar asked Nov 08 '13 16:11

Martin


People also ask

How do I open a navigation drawer?

Android Navigation Drawer is a sliding left menu that is used to display the important links in the application. Navigation drawer makes it easy to navigate to and fro between those links. It's not visible by default and it needs to opened either by sliding from left or clicking its icon in the ActionBar.

Where is the navigation drawer on my Android phone?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

How do I code a navigation drawer for an Android app?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.


2 Answers

I ran into this issue also, assuming you already have an ActionBarDrawerToggle as @Kernald suggested, you need to add the following also to your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

This let's the toggle handle the icon button press on the ActionBar, causing the Drawer to slide out.

like image 141
Rajiv Makhijani Avatar answered Oct 30 '22 16:10

Rajiv Makhijani


Everything you need is described in the Navigation Drawer Guide from Google. Basically, you need to enable the "up" action on the ActionBar:

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

Then you need to bind it to a Toggle:

       mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
like image 7
Marc Plano-Lesay Avatar answered Oct 30 '22 16:10

Marc Plano-Lesay