Created
February 1, 2019 11:08
-
-
Save dynoChris/885d124ef9269caedfbb099e09ff3c49 to your computer and use it in GitHub Desktop.
How to add burger menu button to Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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