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)
Related
Actually I was using One MainActivity and multiple Fragments attached to it.
I want to change Navigation Drawer Menu Icon of toolbar from certain fragments as back icon.And set back pressed event for that icon.
here is my code
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();
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.
I am new to Android development. I want to allow the drawer to be shown and used for navigation when a user is logged in, but hidden when the user logs out. How can this be done? It is currently set to close the drawer and set listener to null, but that just changes the toggle button to an arrow. Thanks!
DrawerLayout drawer = (DrawerLayout) this.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();
drawer.setDrawerListener(null);
drawer.closeDrawer(Gravity.LEFT);
After checking into this further, the following allowed me to hide the ActionBarDrawerToggle:
toolbar.setNavigationIcon(null);
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.
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.