Skip to content

Instantly share code, notes, and snippets.

@dynoChris
Created February 1, 2019 11:08
Show Gist options
  • Save dynoChris/885d124ef9269caedfbb099e09ff3c49 to your computer and use it in GitHub Desktop.
Save dynoChris/885d124ef9269caedfbb099e09ff3c49 to your computer and use it in GitHub Desktop.
How to add burger menu button to Android
//first method (static icon)
onCreate() {
...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
...
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
drawerLayout.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
//second method (dynamic icon, transform to back icon — arrow left)
onCreate() {
...
final DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment