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
}
}
I've been trying some of these examples on my App, but none of them seemed to work. I am using fragments and some of them must show the back option instead of home. Here is my implementation (in Kotlin):
NOTE: these are the overridden onResume and onStop method inside the fragment.
REMARK: the hamburger icon will only show up if the toggle.syncState() method is called. I've been almost 24 hours figuring out why the home icon wasn't showing.
Hope my post can help someone.
If I assume you're using
android.support.v4.widget.DrawerLayout
in your layout, then this approach may work for you; I've only tested onAPI 21
but given it's mostly using the support libraries, it should work (famous last words) on lower or higher targets.The solution uses
ActionBarDrawerToggle.setDrawerIndicatorEnabled
to toggle the visibility of the hamburger icon andActionBar.setDisplayHomeAsUpEnabled
for visibility of the Up button, essentially making use of their respectivedrawable
resources.Other assumptions
Theme.AppCompat.Light.NoActionBar
.-> If you have a drawer on homeactivity and initalfragment you have to show the drawertoggle and after inner fragement you don't want to show the drawer behalf of drawer you have to show the back button and change the title of all fragement like this.
make your actionbartoggle public in your activity.
in your home fragment write this code.
and in other fragments write this code :
and in your main activity write in on backpressed :