How can the ActionBarDrawerToggle be hidden upon user logout? - android

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);

Related

change drawer menu Icon to Back Icon from Fragment

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();

Navigation drawer not opening from cutom menu button

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.

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)

For android's navigation drawer, why setDisplayHomeAsUpEnabled is required

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.

Categories

Resources