How can I call a function when navigation drawer is toggled? - android

I want to change the items on my navigation drawer based on the user that has logged in and if they have premium account or not.
I know how to make items visible and invisible, but I was wondering if I can put a function somewhere that when ever it's toggled it would set the visibility of them items and also change the header based on the user profile.
thank you all in advance.

Add a DrawerListener to your DrawerLayout
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(#NonNull View drawerView, float slideOffset) {
}
#Override
public void onDrawerOpened(#NonNull View drawerView) {
}
#Override
public void onDrawerClosed(#NonNull View drawerView) {
}
#Override
public void onDrawerStateChanged(int newState) {
}
});

I suggest you add a DrawerListener to your DrawerLayout
drawerLayout.addDrawerListener(new DrawerToggle());
DrawerToggle is an extension of android.support.v4.widget.SimpleDrawerListener in which you've overidden onClick() and onDrawerSlide()
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
You can use these methods to catch drawer toggle event

Related

Change toggle button image Icon In Navigation Drawer right to left

I Want to change the navigation drawer toggle icon for close and open.
I downloaded the sample code from this link:
http://vardhan-justlikethat.blogspot.in/2014/02/android-slider-from-right-to-left.html
But there is no method call when navigation drawer closed.
I required to change that toggle button with some other image,
Thanks.
in res/drawable-hdpi/ ,res/drawable-mdpi/ , res/drawable-xhdpi/ and res/drawable-xxhdpi/ you have ic_drawer.png change that with your image
DrawerListener:
DrawerListener drawerListener = new DrawerListener() {
#Override
public void onDrawerClosed(View drawerView) {
drawerHandleImage.setImageResource(R.id.closed);
}
#Override
public void onDrawerOpened(View drawerView) {
drawerHandleImage.setImageResource(R.id.opened);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) { }
#Override
public void onDrawerStateChanged(int newState) { }
};
Set the listener to your DrawerLayout:
drawerLayout.setDrawerListener(drawerListener);

Android v7-sdk21 Hamburger Navigation Drawer toggle without show back arrow

It is possible to just show the Android default hamburger navigation drawer without change to back arrow feature?
The animation you're referring to is handled in the onDrawerOpened(), onDrawerClosed(), and onDrawerSlide() methods of the ActionBarDrawerToggle class. To disable it, simply override those methods without calling through to the super's methods. For example:
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(...) {
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerOpened(View drawerView) {
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
};

NavigationDrawer: no more animation on drawer icon when setting drawer listener

<-- the drawer icon
If you create, with android studio, a project with a navigation drawer, when you open/close the drawer, there will be a smooth animation of the drawer icon.
If I add a drawer listener to my drawerlayout, there is no more animation the drawer icon doesnt change anymore:
DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawer_layout);
dl.setDrawerListener(new ActionBarDrawerToggle(this, dl,R.drawable.ic_drawer,R.string.navigation_drawer_open,R.string.navigation_drawer_close));
I tried to override methods of ActionBarDrawerToggle to add calls to syncState().
DrawerLayout dl = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, dl,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
mDrawerToggle.syncState();
}
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
mDrawerToggle.syncState();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
mDrawerToggle.syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mDrawerToggle.syncState();
}
};
dl.setDrawerListener(mDrawerToggle);
Now, when the drawer is open, I have a small icon, and then it switch to a large icon when the drawer is closed, but I dont have the smooth animation.
Does somebody know how to get the smooth animation?
Try to put the #Override annotations that are missing for the last two listeners and remove all syncState() calls. Call syncState() from your Activity's onPostCreate:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
I have had the same problem.It's been a long time since the question was asked, but if you are still interested, here is my answer.
put
invalidateOptionsMenu();
instead of
syncstate();
for onDrawerClosed(), onDrawerOpened(), onDrawerSlide(), onDrawerStateChanged()
Also, put
#Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
Intilize the boolean as false..private boolean isDrawerOpen = false;
public void onDrawerSlide(View drawerView, float slideOffset) {
if(slideOffset > .55 && !isDrawerOpen){
super.onDrawerSlide(drawerView, 1);
onDrawerOpened(drawerView);
isDrawerOpen = true;
} else if(slideOffset < .45 && isDrawerOpen) {
super.onDrawerSlide(drawerView,slideOffset);
onDrawerClosed(drawerView);
isDrawerOpen = false;
}
}
public void onDrawerClosed(View view) {
getActionBar().setTitle("Your title");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("your title");
invalidateOptionsMenu();
}
};

How to disable drawer layout from one side?

I have made a double drawer layout without an actionbar something like this:
Using Navigation Drawer without TitleBar or ActionBar
My requirement is to disable the drawer on right when drawer on left is open & vice versa. So I'm hiding the right drawer button when left drawer is open & vice versa and that works fine.
But the problem is, even when I hide a button(left or right), the drawer still opens with horizontal swipe(right to left swipe). So how do I prevent the drawer from opening from swipe??
And since I'm doing it without ActionBarDrawerToggle inbuilt functions like
setOnDrawerOpenListener
setOnDrawerCloseListener
are not available.
Please Help!!
this may help you...
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerStateChanged(int arg0) {
}
#Override
public void onDrawerSlide(View view, float arg1) {
}
#Override
public void onDrawerOpened(View view) {
if(view == rightDrawerView) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, leftDrawerView);
} else if(view == leftDrawerView) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, rightDrawerView);
}
}
#Override
public void onDrawerClosed(View view) {
if(view == rightDrawerView) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, leftDrawerView);
} else if(view == leftDrawerView) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, rightDrawerView);
}
}
});
Try this
setDrawerLockMode (int lockMode, View drawerView)
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, yourDrawer)

Hide ActionBar MenuItems when Navigation Drawer slides for any amount

I'm trying to implement a Navigation Drawer that hides the menu items in the ActionBar whenever the drawer is opened.
I am following google's documentation, however their code does not produce the expected behavior.
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Using this code, the menu items are hidden when the drawer becomes completely opened , and shown when the drawer becomes completely closed.
However, the Gmail app behaves differently. The menu items are hidden as soon as the drawer opens by any amount. This is the behavior I want. Does anyone know how to achieve this?
Thanks!
Have you tried this:
Use invalidateOptionsMenu() whenever you toggle the nav drawer, by measuring the sliding offset.
Iterate over each menu item in onPrepareOptionsMenu(Menu menu) and hide it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = shouldGoInvisible;
hideMenuItems(menu, !drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible)
{
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
}
Detecting how much the nav drawer has slided:
mDrawerLayout.setDrawerListener(new DrawerListener(){
float mPreviousOffset = 0f;
#Override
public void onDrawerClosed(View arg0) {
super.onDrawerClosed(arg0);
shouldGoInvisible = false;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View arg0) {
super.onDrawerOpened(arg0);
shouldGoInvisible = true;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View arg0, float slideOffset) {
super.onDrawerSlide(arg0, slideOffset);
if(slideOffset > mPreviousOffset && !shouldGoInvisible){
shouldGoInvisible = true;
invalidateOptionsMenu();
}else if(mPreviousOffset > slideOffset && slideOffset < 0.5f && shouldGoInvisible){
shouldGoInvisible = false;
invalidateOptionsMenu();
}
mPreviousOffset = slideOffset;
}
#Override
public void onDrawerStateChanged(int arg0) {
// or use states of the drawer to hide/show the items
}});
Note: shouldGoInvisible is class field.
If you want to override action bar as soon as drawer enters screen and restore action bar the moment drawer is no longer visible (exactly how Gmail behaves as of March 20, 2014) you can use the following code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
boolean isOpened = mDrawerLayout.isDrawerOpen(mDrawerList);
boolean isVisible = mDrawerLayout.isDrawerVisible(mDrawerList);
if (!isOpened && !isVisible) {
if (newState == DrawerLayout.STATE_IDLE) {
// drawer just hid completely
restoreActionBar();
} else {
// } else if (newState == DrawerLayout.STATE_SETTLING) {
// drawer just entered screen
overrideActionBar();
}
}
}
private void restoreActionBar() {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
private void overrideActionBar() {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
Modify restoreActionBar() and overrideActionBar() methods acording to your needs.
There is no need to distinguish between swipes and home button and no need to measure swipe lengths.
Variation
If you don't want to reference the drawer list view use the following code instead:
boolean isOpened = mDrawerLayout.isDrawerOpen(GravityCompat.START);
boolean isVisible = mDrawerLayout.isDrawerVisible(GravityCompat.START);
You may want to use GravityCompat.END instead depending on what you specified in XML layout.
Edit - concerning actions
The above example does not hide action bar items relevant to content below the navigation drawer. To do so or show different set of icons when drawer is visible you have to keep track of whether the drawer is opened or closed manually.
In addition to the above code declare private boolean mDrawerVisible = false with proper save/restore state handling.
Then modify mDrawerToggle inner methods as follows:
private void restoreActionBar() {
getSupportActionBar().setTitle(mTitle);
mDrawerVisible = false;
supportInvalidateOptionsMenu();
}
private void overrideActionBar() {
getSupportActionBar().setTitle(mDrawerTitle);
mDrawerVisible = true;
supportInvalidateOptionsMenu();
}
Finally in onCreateOptionsMenu inflate different menu resource or in onPrepareOptionsMenu show/hide different actions based on the value of mDrawerVisible.
I'd half agree with Nikola but it's sufficient just to update the icons when the drawer state has
Create a global variable to keep track of the drawer state:
private int mDrawerState;
Set a new DrawerListener:
mDrawerLayout.setDrawerListener(new DrawerListener() {
#Override
public void onDrawerStateChanged(int state) {
mDrawerState = state;
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View view, float slide) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerOpened(View view) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
}
});
Update the menu visibility:
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawer);
for(int i=0;i<menu.size();i++){
// If the drawer is moving / settling or open do not draw the icons
menu.getItem(i).setVisible(mDrawerState!=DrawerLayout.STATE_DRAGGING &&
mDrawerState!=DrawerLayout.STATE_SETTLING && !drawerOpen);
}
I have a better solution for this question:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (navigationDrawerFragment.isDrawerOpen()) {
menu.clear();
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!navigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
showLocalContextActionBar();
return false;
}
return super.onCreateOptionsMenu(menu);
}
If you want to hide all menu items, simply use:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return showActionBarMenu; // boolean value, set it in drawer listeners as class variable
}
Then you do not need to visible of each menu item.
I took the answer by #Laurence Dawson and simplified it a bit. This solution does not require any class members to be used.
Execute this code during onCreate():
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View view, float v) {
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View view) {}
#Override
public void onDrawerStateChanged(int state) {}
});
And override this method:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
boolean actionsVisibility = !drawerLayout.isDrawerVisible(Gravity.START);
for(int i=0;i<menu.size();i++){
menu.getItem(i).setVisible(actionsVisibility);
}
return super.onPrepareOptionsMenu(menu);
}
Few notes:
The above implementation assumes that the view associated with NavigationDrawer has its layout_gravity set to start in XML.
Unrelated to OP's question, but annoying: there seems to be some kind of a bug which causes the drawer to stuck along the way. If you do observe this behavior, here is the solution: Android Navigation Drawer bug using the sample
I have different code but the same solution:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
hideMenuItems(menu, !mShouldGoInvisible);
return super.onCreateOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible){
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
}
and
#Override
public void onNavigationDrawerListener(boolean opened, int position) {
if (opened){
mShouldGoInvisible = true;
invalidateOptionsMenu();
} else {
mShouldGoInvisible = false;
invalidateOptionsMenu();
}
}

Categories

Resources