Skip to content

Instantly share code, notes, and snippets.

@urandom
Last active April 22, 2016 06:37
Show Gist options
  • Save urandom/8608fb45fdd489c8cd2bf9654a2139a1 to your computer and use it in GitHub Desktop.
Save urandom/8608fb45fdd489c8cd2bf9654a2139a1 to your computer and use it in GitHub Desktop.
Taking control of android's drawer toggle
// Where you setup the toolbar
toolbarDelegate = new ToolbarCompatDelegate(toolbar);
slider = new DrawerArrowDrawableToggle(toolbarDelegate.getActionBarThemedContext());
// DrawerLayout.DrawerListener implementation
@Override public void onDrawerSlide(View drawerView, float slideOffset) {
slider.setPosition(Math.min(1f, Math.max(0, slideOffset)));
}
@Override public void onDrawerOpened(View drawerView) {
slider.setPosition(1);
toolbarDelegate.setActionBarDescription(R.string.navigation_drawer_close);
}
@Override public void onDrawerClosed(View drawerView) {
toolbarDelegate.setActionBarDescription(R.string.navigation_drawer_open);
slider.setPosition(0);
}
@Override public void onDrawerStateChanged(int newState) { }
// Changing the slider to an arrow:
slider.setPosition(1)
// Changing the slider to a burger:
slider.setPosition(0)
// position property animator for the slider
private Property<DrawerArrowDrawableToggle, Float> sliderPositionProperty = new Property<DrawerArrowDrawableToggle, Float>(Float.class, "") {
@Override public Float get(DrawerArrowDrawableToggle object) {
return object.getPosition();
}
@Override public void set(DrawerArrowDrawableToggle object, Float value) {
object.setPosition(value);
}
};
// Animating the slider to an arrow:
Animator animator = ObjectAnimator.ofFloat(slider, sliderPositionProperty, 0, 1);
animator.addListener(new AnimatorListenerAdapter() {});
animator.start();
public class DrawerArrowDrawableToggle extends DrawerArrowDrawable {
public DrawerArrowDrawableToggle(Context context) {
super(context);
}
public void setPosition(float position) {
if (position == 1f) {
setVerticalMirror(true);
} else if (position == 0f) {
setVerticalMirror(false);
}
setProgress(position);
}
public float getPosition() {
return getProgress();
}
}
public class ToolbarCompatDelegate implements ActionBarDrawerToggle.Delegate {
final Toolbar toolbar;
final Drawable defaultUpIndicator;
final CharSequence defaultContentDescription;
ToolbarCompatDelegate(Toolbar toolbar) {
this.toolbar = toolbar;
defaultUpIndicator = toolbar.getNavigationIcon();
defaultContentDescription = toolbar.getNavigationContentDescription();
}
@Override
public void setActionBarUpIndicator(Drawable upDrawable, @StringRes int contentDescRes) {
toolbar.setNavigationIcon(upDrawable);
setActionBarDescription(contentDescRes);
}
@Override
public void setActionBarDescription(@StringRes int contentDescRes) {
if (contentDescRes == 0) {
toolbar.setNavigationContentDescription(defaultContentDescription);
} else {
toolbar.setNavigationContentDescription(contentDescRes);
}
}
@Override
public Drawable getThemeUpIndicator() {
return defaultUpIndicator;
}
@Override
public Context getActionBarThemedContext() {
return toolbar.getContext();
}
@Override
public boolean isNavigationVisible() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment