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) {
}
};
Related
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
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);
Once, my drawer icon changed from hamburger to the back button calling this:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
So, the back arrow is shown, but clicking on it , the drawer is still opening.
I would like to be able to handle it, to call onBackpressed() instead.
This is when i make the instance of the drawer
mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
#Override
public View.OnClickListener getToolbarNavigationClickListener() {
return super.getToolbarNavigationClickListener();
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) return;
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true");
}
getActivity().invalidateOptionsMenu();
}
};
I suppose that getToolbarNavigationClickListener() method must be called when I tap on the drawer toogle, no matter if its hamburger or back arrow, but it does not.
When you need to override Arrow on toolbar click, set navigation click to toolbar.
getSupportActionBar().setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//handle back press or open drawer etc.
}
});
It works for me every time.
I want to animate the drawer icon from a burger to an arrow and vice versa manually, not only when drawer is being dragged, is it possible? I'm using support library appcompat-v7:21.
Also I can't find the source code of android.support.v7.app.ActionBarDrawerToggle which would helpful.
I found a way to animate the icon with a simple ValueAnimator and .onDrawerSlide method.
ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
drawerToggle.onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(300);
anim.start();
But maybe there is a better solution.
// define your drawer layout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// define your actionbar toggle
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
// Pass in context
this,
// Pass in your drawer layout
drawerLayout,
// Pass a String resource to describe the "open drawer" action for accessibility
R.string.open_drawer,
// Pass a String resource to describe the "close drawer" action for accessibility
R.string.close_drawer
);
// add a drawerListener to your drawer layout
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Here's where the animation happens.
// the provided float starts at 0 (drawer closed) and goes to 1 (drawer open) hitting all points in between.
// ActionBarDrawerToggle has an onDrawerSlide method that takes your drawer layout and the provided float from the onDrawerSlide constructor.
// By passing the slideOffSet from the drawer's OnDrawerSlide method into the toggle, it maps the animation to the provided float.
toggle.onDrawerSlide(drawerView, slideOffset);
}
#Override
public void onDrawerOpened(View view) {
}
#Override
public void onDrawerClosed(View view) {
}
#Override
public void onDrawerStateChanged(int i) {
}
});
EDIT: The above will work, but I found a much more elegant way to do this. By setting the toggle as the drawer's listener, the states are handled by default. See below:
// define your drawer layout
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// define your actionbar drawertoggle
drawerToggle = new ActionBarDrawerToggle(
// Pass in context
this,
// Pass in your drawer layout
drawerLayout,
// Attach the toolbar
toolbar,
// Pass a String resource to describe the "open drawer" action for accessibility
R.string.open_drawer,
// Pass a String resource to describe the "close drawer" action for accessibility
R.string.close_drawer
) {
// attach drawer listener states to the ActionBarDrawerToggle
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
<-- 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();
}
};