I am having the following problem:
I know how to set up a toolbar to show a back button icon instead of a burger button icon.
From this:
to this:
using: getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Now, I want to do the reverse action, I want to go from back button icon to burger icon:
to here:
How can I do this?
Update:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
private void enableViews(boolean enable) {
if(enable) {
// Enables back button icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
// TODO: Enables burger icon
}
}
For me, I wanted to change the Burger icon by a Back Arrow icon on the left side of
Fragment
's ActionBar as I am using aNavigation Drawer
. Also Adding a Menu on the right side.In Main Activity, it's already set - by default when Android Studio create the Navigation Drawer for me - like this:
The issue is how to customize the
ActionBar
in theFragment
, so when I go to theFragment
it shall show me the customizedActionBar
and when the Back Arrow icon is clicked, it shall leave the fragment and theActionBar
should back to the first state.In Fragment (Complete Implementation):
fragment_actionbar.xml
ic_menu_back.xml
fragment_menu.xml
I found flexible solutions in The Google I/O 2017 Android App.
So the usage is really simple.
Try adding below code of
style
AppTheme
to youractivity's theme/style.xml
it will make yourhamburger icon
toback icon
withanimation
.Condition if you are using hamburger icon with
NavigationDrawer
andAppCompatActivity/ActionBarActivity
I hope it helps! or you just have to do it with drawable only.
Check this link
The selected answer is too hacky in my opinion.
I tried implementing it, and while doing so I realized that there's actually no good use for the
ActionBarDrawerToggle
(maybe that is why it was removed from the official android tutorial regarding Navigation Drawer): it doesn't make your life easier when you want to co-ordinate between the navigate drawer and the action bar.The problem is that you only have 1 home "button", and that it has 2 different functionalities - open the drawer when you are in the main screen, and go up when you are further down in your app. Passing the toolbar as a parameter to
ActionBarDrawerToggle
constructor, adds the menu icon to it, and calls openDrawer on click event. Now if you want to switch to an up event you have to turn off this special icon, and re-enable the inherent back functionality of the action bar... which is still a mess.So if
ActionBarDrawerToggle
doesn't help you (yet, maybe someone will figure out a way where it does), why use it in the first place? Here's how to do it without it:Use this
you can change the action bar button with: