I want to change the Hamburger icon on Android ActionBar to Up arrow on button click, and also to change the Up arrow back to Hamburger icon on another button click event.
I have tried using ,
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
However, none of them works. I can change the Hamburger icon to up arrow easily using,
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But not the vice-versa.
Also, how do I override the home button control?
I mean when a certain layout is open, I want the up arrow to close that layout on click and not to call the navigation drawer.
Please help.
You could use the following piece of code:
ViewGroup home =(ViewGroup)findViewById(android.R.id.home).getParent();
// get the first child (up imageview)
((ImageView) home.getChildAt(0)).setImageResource(your image here);
Sorry, no privileges to comment so updating the answer. Well, since this is an imageView, I am assuming you could place an onClickListener and toggle between using the hamburger and the back arrow. This should be close to the material design's animated hamburger behavior.
Related
I try to hide ActionBar in Navigation View by using getSupportActionBar().hide() but it makes menu icon on top left dissapear too, like this :
I want it show like this :
Could someone give me some advice ?
No, you can't because the icon is part of ActionBar. You can create ImageView (or Button, ...) and place it at the top-left position and handle click event or use transparent ToolBar
Add a ImageButton and fix it's position to top left corner . And handle the click event of the button using OnClickListner ... Simple ...
I have just done it,for anyone need it, just create an ImageView and handle click event with this:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.openDrawer(Gravity.LEFT);
I want to add title under the icon that as default appears when i create the navigation drawer .
like in red border
Its not possible if you use normal ActionBarToggle, you can provide your own custom layout in toolbar and add menu icon to it but back animation will not work. Also you will have to handle click events on the menu your self
When I use to change Hamburger Icon to Back Icon while add a new fragment it's Perfectly working. Here is my code
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
supportActionBar!!.setDisplayShowHomeEnabled(true)
But when I press back button and close the fragment then it not change to hamburger icon
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
supportActionBar!!.setDisplayShowHomeEnabled(false)
NOTE: I dont have drawer layout. I use this library: yarolegovich/SlidingRootNav
This is expected behaviour, it won't automatically change when you are doing:
actionBar.setDisplayHomeAsUpEnabled(true);
One solution would be to handle it for yourself. Just change the icon manually
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back);
and on back pressed, change it to hamburger icon:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.hamburger_icon);
Hope this helps. Cheers.
I have an activity A and on that activity, I want to create a new Activity B (or fragment if needs be). On A, I have a hamburger icon and on B, I have left arrow icon. When I created B from A, B is created with left arrow icon but there is no animation. I want to turn hamburger icon to left arrow icon animated as below when creating new activity.
It should work like android gmail application. In Gmail app, when you are in inbox menu, there is a hamburger icon in toolbar and if you click and open one of your mails in inbox list, hamburger icon turns to left arrow animated. When you click to left arrow or back button, the left arrow turns to hamburger icon also animated. Any help?
I can see 2 ways of implementing it. The right way to do this is to use the Toolbar or ActionBar as a shared element between activities and change the state of ActionBarToggle when activity is started and when you return from that activity. But that will not work in pre-lollipop devices. But if you really badly want that to work on older devices, you can start the animation in the Toolbar inside Activity B after the activity B starts, it will not look as smooth as shared element transition, but that is the most straightforward way I see to implement it on older devices.
I'm using MaterialDrawer library to implement a basic material drawer in my app.
I wasted more than two days trying to figure out how to transform the menu toggle button (burger) to a back button, like Gmail app when you open an email.
In Gmail app when you open an email the menu icon is animated into a back arrow but the menu drawer remains still available.
I've tried to use ValueAnimator to animate the icon as explained here.
I've setted an onAnimationEnd listener to replace the menu icon with a working back button:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
After doing that the menu drawer slide position get unlinked from the menu drawer icon animation, and when i try to restore the menu drawer button and animate it backwards it gets stuck:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
Actually a similar question has been already asked, and has been marked has duplicated when it is not: Navigation Drawer with Burger to back arrow animation on Lollipop