For android's navigation drawer, why setDisplayHomeAsUpEnabled is required - android

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.

Related

Disable ActionBarDrawerToggle's drawer indicator, but keep the hamburger icon

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.

Add NavigationDrawer toggler by replacing back navigation in action bar

I have an activity in my app which is not launcher.
I want to add a navigation drawer with toggler. I tried following the instructions here http://developer.android.com/training/implementing-navigation/nav-drawer.html#ActionBarIcon
But my back navigation still shows up (It exits the app as I have added appropriate flags to the intent)
How can I show the drawer toggler icon ?
Also android.support.v4.app.ActionBarDrawerToggler is deprecated, what should I use instead ?
for android.support.v4.app.ActionBarDrawerToggler is deprecated to use new one get support library supporv7 appcompact check here https://developer.android.com/intl/es/reference/android/support/v4/app/ActionBarDrawerToggle.html
and creating Navigation drawer here you get all information
http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html
code sample
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
use the latest support library you will get done it easily and not use setHomeasUpEnabled(true)

Problems with icon_drawable in navegation Drawable

I am trying to show Icon_drawble in a ActionBar, but when R.drawable.ic_drawer is in the first position show return arrow in actionbar.
like this:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
I need to show something like this, but the code above It doesnt work.
If I change R.drawable.ic_drawer to another position,it give me the follow logcat error.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.drawer_open,
R.drawable.ic_drawer,//another position
R.string.drawer_close
)
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navegatiodrawer/com.example.navegatiodrawer.MainActivity}: android.content.res.Resources$NotFoundException: File Open navigation drawer from drawable resource ID
This appears to be a bug in recent versions of the AppCompat library.
Part of the confusion is likely due to the fact that Android Studio's new project wizard generates bad code when you create a new navigation drawer activity- it uses the v4 support library ActionBarDrawerToggle, which is deprecated. Instead, it should use the v7 support library ActionBarDrawerToggle.
You have two options:
The best option is to switch to the v7 ActionBarDrawerToggle. To do this, change your imports to use android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle. The only other change you should need to make is removing your ic_drawer parameter altogether- the newer version of the toggle generates the hamburger you are looking for automatically.
If you insist on using the v4 toggle or a custom icon, you can revert to an older version of the support library. Using the default generated project when creating a new navigation drawer activity, I was able to sidestep this bug by reverting to com.android.support:appcompat-v7:22.1.0 in my build.gradle.
There is already a bug in the issue tracker for updating the wizard-generated code. I wouldn't expect the v4 toggle to get fixed since it is deprecated.

How to Replace the app icon with the new drawer animation on API19 and lower devices?

I have a problem with the new Animation from the drawer look here on the G+post from me: Link to the Picture
How can I replace the app icon with the new drawer animation on API19 and lower devices? I want it to look like the ones from PlayStore, Newstand, etc.
You're going to need the new support V7 library and will need to set the Action Bar Drawer Toggle as such - with this being your activity/context:
ActionBarDrawerToggle navigationToggle = new ActionBarDrawerToggle(this,navigationDrawerLayout,R.string.nav_drawer_open,R.string.nav_drawer_closed);
navigationDrawerLayout.setDrawerListener(navigationToggle);
Where ActionBarDrawerToggle is inherited from android.support.v7.app.ActionBarDrawerToggle
It is absolutely necessary that your new Activity extend ActionBarActivity provided by android.support.v7.app.ActionBarActivity otherwise the action bar will not show up. You'll likely have to adjust all your action bar references from getActionBar() to getSupportActionBar(). Let me know if there's anything I can help with as I just transitioned 2 apps into the new guidelines using the SupportV7 library.
What is stated by Logan is correct but it does not require that much changed.
If you already have a drawer then you will most likely have code similar to this.
import android.support.v4.app.ActionBarDrawerToggle;
//some other code
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout,
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open,
R.string.drawer_close)
If so then all you need to do is to remove the line that is comment above, and change to the v7 library, so that you get something like this.
import android.support.v7.app.ActionBarDrawerToggle;
//some other code
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close)
There is no need to change to inherit anything other than the standard Activity; and using a theme such as holo.light.darkactionbar or any other action bar related theme works just fine.

Why same Navigation Drawer is not Toggling from another activity in android?

I want to use same navigation drawer in multiple activities so I have implemented navigation drawer in MainActivity and extended this MainActivity in other Activities in which I want to use the same navigation drawer.
When i Touch the Drawer Icon, it is highlighting but the drawer is not opening.
I have followed the code explained in this link: Same Navigation Drawer on different Activities
Make sure you create your ActionBarDrawerToggle.
I personally do it in Activity.OnCreate() like this:
if (mDrawerLayout != null) {//for large screens it is null
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, 0, 0);
mDrawerLayout.setDrawerListener(this);
}
So in my case I should call super.onCreate() in the descendant class.
Edit
You may also look if all the methods of DrawerListener are overridden correctly.
Still it's not a standard way to use Navigation Drawer. Make sure if your design is what you really need - fragments are more flexible and easy to use. Here is another related post.

Categories

Resources