3

I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and hamburgerbar:

enter image description here

Hamburger bar

enter image description here

I can open this bar by sliding it but I also want to make it open by clicking on drawable icon (right top corner icon). How can i do that?

MainActivity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

I don't think so that I need to do some changes in layout files. What do I have to add in MainActivity file to make it possible?

I am newbie in Android code. Any help will be appreciable.

Community
  • 1
  • 1
  • check this will help you http://stackoverflow.com/questions/17821532/how-to-open-navigation-drawer-with-no-actionbar-open-with-just-a-button – Pavan Sep 08 '15 at 14:57

3 Answers3

0

Use the openDrawer() method.

private DrawerLayout mDrawerLayout;
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
mDrawerLayout.openDrawer(Gravity.END); // or whatever gravity the of the drawer you want to open
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Sorry! I didn't get you. How it will help me to open bar by clicking drawable icon (`R.drawable.icon_navigation`)? –  Sep 08 '15 at 15:00
  • 1
    I think Gravity.END better suits his needs since he uses it on the right side. – Mike Sep 08 '15 at 15:01
  • @SnowClue Call `openDrawer()` inside of the `OnClickListener` – Karakuri Sep 08 '15 at 15:08
0

Using the Toolbar component should be fairly easy to achieve this by using a similar code to this:

    Toolbar toolbar = (Toolbar) findViewById(R.id.home_toolbar);
    toolbar.inflateMenu(R.menu.menu_home);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item.getItemId() == R.id.action_settings) {
                mDrawerLayout.openDrawer(Gravity.RIGHT); // where mDrawerLayout is your android.support.v4.widget.DrawerLayout in your activity's xml layout.
            }
            return false;
        }
    });

EDIT:

The key component here is the menu_home.xml file which goes to your res/menu folder. You can add your desired menu item there, customize it's icon and even more, add as many items as you'd like to have on the right side of the toolbar(Obviously handle the openDrawer() method on whichever menu item you need - the recommended one is the rightmost though).

Mike
  • 4,550
  • 4
  • 33
  • 47
  • It will open the bar by clicking toolbar anywhere. right? –  Sep 08 '15 at 15:30
  • It shouldn't open the drawer if you tap anywhere on the toolbar. That's a really bad practice and a wrong UX. It should only open if you tap the hamburger icon. – Mike Sep 08 '15 at 16:39
  • But where are you passing `hamburger icon` in above code? –  Sep 08 '15 at 16:56
0

Use Activity's onOptionsItemSelected(MenuItem menuItem) method:

First of all, keep the reference to your DrawerLayout in a class field:

DrawerLayout drawerLayout;

Somewhere in onCreate put this:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)

And implement the method:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    // if you want the default back/home hamburger menu item just put android.R.id.home instead
    if (menuItem.getItemId() == R.drawable.icon_navigation) { 
        drawerLayout.openDrawer(GravityCompat.END);
    }
    return super.onOptionsItemSelected(menuItem);
}
maciekjanusz
  • 4,702
  • 25
  • 36