How to return animation of humberger icon - android

I just make my first steps in android programming and i need a help!
I created a navigation drawer and it opened/closed with animation of humberher icon automatically. Then i decided to make damping effect to toolbar. My toolbar became dark. I used code below. After that i had that effect but animation of humburger icon disappeared. I don`t understand what happened. Can anyone help me to solve this problem. I want to return animation of humberger icon.
public void onDrawerSlide(View drawerView, float slideOffset) {
if(slideOffset<0.6){
toolbar.setAlpha(1 - slideOffset);
}
}

The animation you're describing is handled in ActionBarDrawerToggle's onDrawerSlide() method. You've overridden the method without calling through to the super's method, so the animation code isn't running. Simply add the super call to your method.
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if(slideOffset<0.6) {
toolbar.setAlpha(1 - slideOffset);
}
}

Related

Android DrawerLayout Lagging

The DrawerLayout take noticeable time to close
here is my code:
#Override
public void onDrawerOpened(View drawerView) {
if (items.size() == 0)
view.setVisibility(View.GONE);
else view.setVisibility(View.VISIBLE);}
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
is there a solution for that?
Your problem is that the onDrawerOpened() method doesn't fire until the drawer is completely open. The perceived delay is due to the View being visible during the opening.
One solution would be to disable dragging the drawer, and only allow the toggle to open and close it. You could then do your size check before opening it programmatically. This, though, would require locking the drawer to the appropriate state in each of the onDrawerOpened() and onDrawerClosed() methods, and, of course, you'd lose that standard mode of interaction.
The preferred option is probably to do the check and visibility setting as soon as possible as the drawer is starting to open. We can do this in the onDrawerSlide() method instead, keeping a boolean flag to determine if the drawer is sliding after having been closed. For example:
actionBarDrawerToggle = new ActionBarDrawerToggle(...) {
private boolean opened;
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset == 0) {
opened = false;
}
else {
if (!opened) {
opened = true;
if (items.size() == 0) {
view.setVisibility(View.GONE);
}
else {
view.setVisibility(View.VISIBLE);
}
}
}
}
...
}
have you include
actionBarDrawerToggle.syncState()
What does syncState() exactly do?
Well, ActionBarDrawerToggle.syncState() will synchronize the changed icon's state, which deponds on actions of DrawerLayout. If you ever tried to remove the syncState(), you will realize that the icon of arrow won't rotate any more.

how to change actionbar hamburger icon to back icon (with animation) from activity

No, its not a re asked question
I've searched so well. but nowhere found how to do this. I want to change the hamburger icon to back icon by calling a method from my activity. I can do it with Drawer.. but I wanna do it without opening drawer..
for example, when a fragment is shown.. the hamburger will turn into back arrow(with animation)
then clicking the back arrow will hide fragment and turn the arrow back into hamburger.
I want the animation while doing this.. is there any way to do that? or its only possible while opening/closing drawer
Thanks in advance
I did it by Calling
onDrawerSlide(View v, float f);
where float value from 0.0f to 1.0f defines the progress of transforming from hamburger to back arrow.
Then I used object animator to call this method 100 times each time float value 0.1 increased
and the magic happened.
Thank you #gauravsarma for showing me the way.
Do the following
ActionBarDrawerToggle toolbarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(toolbarDrawerToggle);
toolbarDrawerToggle.syncState();

Android navigation drawer onDrawerSlide is not called

I've put following code into onDrawerSlide method of navigation drawer in order to move content when drawer is opening.
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
container.setTranslationX(slideOffset * drawerView.getWidth());
mDrawerLayout.bringChildToFront(drawerView);
mDrawerLayout.requestLayout();
}
It works perfectly almost always, except of first app run - I open drawer programmatically to teach user using it.
So, when I'm calling
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
drawer is opening, but onDrawerSlide is not called.
I suppose, reason is that something does not have enough time to load, but I don't know what exactly and how escape this.
It works when I'm calling openDrawer in a few seconds after run (via Handler), but it looks terrible on UI.
PS. Tried to call openDrawer in onCreate, onResume, in calling Activity - still no result.
PPS. Also I'm unable to call onDrawerSlide directly - because I don't know slideOffset value.
I think when you open drawer programmatically to teach user using it, you need to call DrawerLayout.DrawerListener.onDrawerSlide(View drawerView, float slideOffset) with a Handler or an AsyncTask which will change in time the value of slideOffset which is in [0, 1].
Just like:
private Handler mUIHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case UPDATE_SLIDING_INDEX:
double slideOffSet= (double)msg.obj;
if (slideOffSet> 0.0 && slideOffSet<= 1.0) {
drawerListener.onDrawerSlide(drawer, slideOffSet);
Message message = mUIHandler.obtainMessage();
message.what = UPDATE_SLIDING_INDEX;
message.obj = slideOffSet- 0.1;
mUIHandler.sendMessageDelayed(message, 100);
} else {
//To do something here.
}
break;
default:
break;
}
}
};
Then you call like this:
Message message = mUIHandler.obtainMessage();
message.what = UPDATE_SLIDING_INDEX;
message.obj = 0.8;
mUIHandler.sendMessage(message);
when mDrawerLayout.setDrawerListener(....); is called before or after of the below if condition.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
If it is after, obviously the onDrawerSlide won't be called.
If it is before then you should debug.

Disable NavigationDrawer icon's slide animation

Is there any way to disable sliding animation of the NavigationDrawer's icon? I mean the effect, when you click the icon or open the Drawer with slide gesture. (I still use the ActionBar at the moment)
Preview
You need to override the onDrawerSlide method of ActionBarDrawerToggle and set the slideOffset to 0 if the drawer is right drawer. So this would disable the animation of the navigation drawer image.
#Override
public void onDrawerSlide(View drawerView, float slideOffset)
{
if(drawerView!=null && drawerView == rightDrawerListView){
super.onDrawerSlide(drawerView, 0);
}else{
super.onDrawerSlide(drawerView, slideOffset);
}
}
Look at this answer. Since support v7 version 25.3.0, you can disable the animation, you can disable the animation using one line of code.
yourActionBarDrawerToggle.setDrawerSlideAnimationEnabled(false);

Problems with Drawer Navigation in Android

I was using the Drawer Navigation which provide by android developer website. The things were I have one of my menubar at the bottom then when I slide the sliding menu out then it was cover my bottom menubar, and that was the problem. Plz help
You can translate your menu like this :
public void onDrawerSlide(View drawerView, float slideOffset)
{
float moveFactor = (yourDrawer.getWidth() * slideOffset);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
if(drawerView.getRight()==moveFactor) {
yourMenu.setTranslationX(moveFactor);
}
}
}

Categories

Resources