Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open navigation drawer by clicking the app icon

I want to let my user open the navigation drawer by clicking the app icon. This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    // Show the Up button in the action bar.

    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.left_drawer);
    ActionBarDrawerToggle 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(R.string.title_activity_add);
        }

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


    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
    getActionBar().setHomeButtonEnabled(true);

}

But when I tap the icon, nothing happens. Where is the problem.

like image 387
user2971688 Avatar asked Nov 10 '13 11:11

user2971688


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?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

What is navigation drawer app?

Navigation drawers provide access to destinations and app functionality, such as switching accounts. They can either be permanently on-screen or controlled by a navigation menu icon. Navigation drawers are recommended for: Apps with five or more top-level destinations.


2 Answers

Have a look here for an example of the docs. You need additional code in

  • onPostCreate() to sync your Drawer state
  • onOptionsItemSelected() to handle the touch event of the App icon
  • onConfigurationChanged() to provide the new configuration to the drawer

    public class YourActivity extends Activity {
    
    public ActionBarDrawerToggle mDrawerToggle;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mDrawerToggle = new ActionBarDrawerToggle();
        ...
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
         super.onPostCreate(savedInstanceState);
         // Sync the toggle state after onRestoreInstanceState has occurred.
         mDrawerToggle.syncState();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    
    @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);
    }
    
    }
    
like image 107
Steve Benett Avatar answered Oct 25 '22 22:10

Steve Benett


You should override onOptionsItemSelected of the activity and use this:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
}
like image 31
moallemi Avatar answered Oct 25 '22 23:10

moallemi