Last active
March 23, 2016 18:06
-
-
Save dakdroid/7a00b0fbfcb40aec9c5c to your computer and use it in GitHub Desktop.
Android's Snackbar doesn't play nice with translucent navigation. This adds a view below the Snackbar, adds a bottom margin with height = navBarHeight to the Snackbar and animates the view in before the Snackbar animates in. It animates out once the Snackbar is off the window. In action: https://plus.google.com/u/0/photos/109720416927515295704/a…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. wrap coordinator layout in Relativelayout | |
2. add the view | |
3. call makeSnackNow(...) | |
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.design.widget.CoordinatorLayout | |
android:id="@+id/coordinator" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_above="@+id/fuckThatHalfBackedImplementation"> | |
<!-- Your stuff should be here --!> | |
</android.support.design.widget.CoordinatorLayout> | |
<View | |
android:id="@+id/fuckThatView" | |
android:layout_width="match_parent" | |
android:layout_height="0px" | |
android:layout_alignParentBottom="true" | |
<!-- in android design library --> | |
android:background="@color/snackbar_background_color" | |
android:visibility="invisible" /> | |
</RelativeLayout> | |
<ListView | |
android:id="@+id/left_drawer" | |
android:layout_width="260dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:choiceMode="singleChoice" | |
android:divider="#00b2dfdb" | |
android:dividerHeight="0dp" /> | |
</android.support.v4.widget.DrawerLayout> | |
*/ | |
private static void makeSnackNow(View coordinatorLayout, final String messageText, final int length, final String actionText, final View.OnClickListener listener, boolean translucentNavigation, final View fuckThatView) { | |
final Snackbar snackbar = Snackbar.make(coordinatorLayout, messageText, length); | |
if (actionText != null && listener != null) { | |
snackbar.setAction(actionText, listener); | |
} | |
if (!translucentNavigation) { | |
snackbar.show(); | |
} else { | |
boolean hasParent = false; | |
for (Snackbar snackbar1 : snackbars) { | |
if (snackbar1.getView().getParent() != null) { | |
hasParent = true; | |
break; | |
} | |
} | |
if (hasParent) { | |
//if a snackbar has a parent then it is showing, try again in 100 ms, else show | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
makeSnackNow(messageText, length, actionText, listener); | |
} | |
}, 100); | |
} else { | |
final int navHeight = Utils.getNavigationBarHeight(getContext()); | |
snackbars.add(snackbar); | |
// from: http://stackoverflow.com/a/31030520/1386748 | |
snackbar.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { | |
@Override | |
public void onViewAttachedToWindow(View v) { | |
} | |
@Override | |
public void onViewDetachedFromWindow(View v) { | |
final Animation a = new Animation() { | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
fuckThatView.getLayoutParams().height = (int) ((1 - interpolatedTime) * navHeight); | |
fuckThatView.requestLayout(); | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
a.setDuration(100); | |
a.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
animation.setAnimationListener(null); | |
fuckThatView.setVisibility(View.INVISIBLE); | |
snackbars.remove(snackbar); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
fuckThatView.startAnimation(a); | |
} | |
}); | |
Animation a = new Animation() { | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
fuckThatView.getLayoutParams().height = (int) (interpolatedTime * navHeight); | |
fuckThatView.requestLayout(); | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
a.setDuration(100); | |
a.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
fuckThatView.getLayoutParams().height = 0; | |
fuckThatView.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
animation.setAnimationListener(null); | |
snackbar.show(); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
fuckThatView.startAnimation(a); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you found any better solution since posting this ~ 7 months ago? I just encountered the same problem in a project