I'm on Lollipop (minSdk=21) and using the v7-appcompat ActionBarDrawerToggle together with the Toolbar, like this (onCreate()):
setActionBar((Toolbar) findViewById(R.id.toolbar));
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.action_open_drawer, R.string.action_close_drawer);
drawerLayout.setDrawerListener(drawerToggle);
I also have the syncState() call in onPostCreate() etc. Generally, it all works well, and the hamburger-to-arrow spinning animation works fine.
I noticed, though, that the icon is 'square' and the press animation ripple is restricted to it. However, certain other apps, like the Android photos app, have the hamburger-to-arrow icons not restricted, i.e. the ripple effect goes outside the actual square area reserved for the icon.
How can I achieve something similar in my app?
Related
I have two activities in my app and both have navigation drawers implemented. Now the scenario is that the Drawer toggle is getting displayed for the first activity but for the same code its not getting displayed for the second activity (which is getting called from the first activity). I don't have much experience in Android Programming and I am stuck on this one. Please help anyone :(
Here's the Java Code snippet to display drawer toggle button:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_quiz);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
You have to get your Toolbar and set the support action bar like that
setSupportActionBar(toolbar);
Ok after struggling for 2 days I finally found what the problem was. drum rolls please ....
The problem was : Z INDEX OF THE CUSTOM ACTION BAR LAYOUT INSIDE DRAWER LAYOUT.
You heard it .. if you are facing similar issues like for example your custom action bar layout is not displaying or your drawer toggle button is not displaying the first thing you should check if your action bar layout is placed down below all your main layouts to increase its z order.
In my case I placed it above my main layouts in the drawer layout. And the funny thing is Android Studio's preview window was still displaying the custom action bar as if it doesn't even care about the z index.
I'm working on an Android App, I have a navigation drawer over there. Since the navigation drawer toolbar can't be transparent, and the ending three dots button icon can't be changed, I opted for hiding that toolbar, and show my custom layout. It will give me all the functionality what ever is needed.
But the problem I'm facing right now is, once the activity starts, if I click the custom menu button it doesn't open. Once I drag it and open, after that whenever I click the menu button it opens the navigation drawer.
What might i be missing? This is what I'm doing, while debugging its even coming to the else part, but doesn't open.
In BaseActivity:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ivLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.LEFT)) {
drawer.closeDrawer(Gravity.LEFT);
} else {
drawer.openDrawer(Gravity.LEFT);
}
}
});
In any of the child activity:
toolbar.setVisibility(View.GONE);
navigationView.setVisibility(View.GONE);
Please help..
The root cause of your problem is the fact that you're setting the drawer View's visibility to GONE. The direct cause of the odd behavior you describe, though, is due to how DrawerLayout and one of its helper classes update the child Views when the drawer state changes.
The OnClickListener you set to open and close the drawer was working as it should. It just didn't appear to be, since the drawer View was GONE. When you manually opened the drawer by dragging, however, the ViewDragHelper that DrawerLayout uses was firing a callback method that explicitly sets the drawer to VISIBLE. This callback is not fired when the drawer is opened programmatically - that is, with the openDrawer() method - which explains why the drawer didn't show just by clicking your custom toggle button. After you had dragged the drawer open once, the drawer View was visible, and the toggle would then work as expected.
The drawer View is in its closed state by default, so you don't need to hide it, and you can just remove the navigationView.setVisibility(View.GONE); line.
In my main activity I'm getting references to my DrawerLayout and Toolbars like this:
//Set the toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Set navigation drawer
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
hamburger = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(hamburger);
hamburger.syncState();
In one of my fragments, I want to completely disable 1) swiping to open the nav drawer and 2) the hamburger/toggle button to toggle nav drawer opening
Currently, I'm doing it like this:
mainActivity = (MainActivity)getActivity();
mainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mainActivity.hamburger.setDrawerIndicatorEnabled(false);
mainActivity.hamburger.syncState();
Swiping is correctly handled - it no longer opens the drawer.
The hamburger icon has completely disappeared though. Ideally, I want the hamburger icon to remain on the screen, but to just sit in a disabled state so that it doesn't do anything when clicked. Is there an alternative to setDrawerIndicatorEnabled that will work this way?
The simplest way to do this is probably to just set a DrawerArrowDrawable as the toggle's Up indicator, and enable/disable the drawer indicator as usual.
After you've initialized the ActionBarDrawerToggle, call:
hamburger.setHomeAsUpIndicator(new DrawerArrowDrawable(toolbar.getContext()));
DrawerArrowDrawable is actually what the ActionBarDrawerToggle uses for that animation, and its default state is the hamburger. When you disable the drawer indicator, it switches to the Up indicator, but that doesn't receive the drawer opening/closing calls, so it just sits in the default state. Re-enabling the drawer indicator switches back to the toggle, which does get the drawer events, so the animation is re-enabled as well.
My application uses ActionBarCompat library as well as the NavigationDrawer support library.
I use ActionBarDrawerToggle appcompat v7 to get the drawer. There are a custom search view on ActionBar. Like this:
But the drawer indicator shows wrongly, doesn't show the Back Arrow when action search view is expanded;
I want it to show like PlayStore application:
How can I do it? Thanks in advance.
There is simple and quick solution to this.
First, you should know now the android.support.v4.app.ActionBarDrawerToggle is deprecated.
You must replace that with android.support.v7.app.ActionBarDrawerToggle.
Here is an Example showing the same.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
After that use support Action Bar as shown here in this documentation and then your drawer indicator will show correctly also showing the Back Arrow when action search view is expanded.
Remember to use com.android.support:appcompat-v7:21.0.+" (Api level 21)
and android.support.v7.app.ActionBar
You can set up the support library using this guide.
AND after that your drawer indicator will perfectly look like this..!!!
You got that issue because you set (android:)homeAsUpIndicator in your theme. Remove that attribute will solve your problem.
This is not the arrow from ActionBarDrawerToggle. I think Google uses Toolbar as they do in google io app. On OnClick event they just change toolbar navigation icon with toolbar.setNavigationIcon(R.id.ic_up). And R.id.ic_up - is a custom arrow drawable.
For me none of solutions posted here in SO worked. I had to look under the hood of support library to find out why and when is the home icon set and I noticed few things. The main observation is that the icon is set in this function:
android.support.v7.widget.Toolbar#setNavigationIcon(android.graphics.drawable.Drawable)
on the line
mNavButtonView.setImageDrawable(icon);
If you are facing the same problem as I was and none of suggested solutions work (setting theme, trying to call setNavigationIcon on toolbar, setHomeAsUpIndicator on Actionbar or even something else), I suggest to locate function mentioned above and put breakpoint there. You can then see when the function is called and identify the last function call (from the Frame window in android studio) that sets up the icon. For me it was this activity life-cycle method syncing navigation drawer:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
mToolbar.setNavigationIcon(R.drawable.ic_hamburger);
}
I simply added last line and it worked.
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.open_drawer,
R.string.close_drawer
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
This is part of my code I'm using to set the navigation drawer and the app icon to toggle it.
I'm very confused in that why is it required to setDrawerListener when already while constructing the DrawerToggle object we have specified the DrawableLayout in the constructor.
Second, why is the setDisplayHomeUpEnabled required when we are actually not enabling it. And without it, why isn't the ic_drawer displayed?
Thanks.
From documentation link
ActionBarDrawerToggle can be used directly as a DrawerLayout.DrawerListener, or if you are already providing your own listener, call through to each of the listener methods from your own.
So setDrawerListener method is for setting drawer listener, it could be custom one or you could use your ActionBarDrawerToggle object because it already implements DrawerLayout.DrawerListener.
Setting setDisplayHomeUpEnabled is showing < sign in your action bar but when using navigation drawer it shows three lines.
This is how it is implemented.