Skip to content

Instantly share code, notes, and snippets.

@BVantur
Last active February 13, 2021 14:58
Show Gist options
  • Save BVantur/935d537170986fc03eb887330405ad75 to your computer and use it in GitHub Desktop.
Save BVantur/935d537170986fc03eb887330405ad75 to your computer and use it in GitHub Desktop.
SnackbarUtils
object SnackbarUtils {
fun showRegular(activity: Activity, textMessage: String) {
show(activity, textMessage)
}
fun showSuccess(activity: Activity, textMessage: String) {
show(activity, textMessage, Color.GREEN)
}
fun showError(activity: Activity, textMessage: String) {
show(activity, textMessage, Color.RED)
}
fun showWarning(activity: Activity, textMessage: String) {
show(activity, textMessage, Color.argb(255, 255, 165, 0))
}
private fun show(
activity: Activity,
textMessage: String,
backgroundColor: Int = ContextCompat.getColor(activity, R.color.teal_200)
) {
val snackbar = Snackbar.make(
activity.findViewById(android.R.id.content),
textMessage,
Snackbar.LENGTH_LONG)
snackbar.view.setBackgroundColor(backgroundColor)
setCorrectAnimationAndPosition(snackbar)
snackbar.show()
}
private fun setCorrectAnimationAndPosition(snackbar: Snackbar) {
val params = snackbar.view.layoutParams
if (params is CoordinatorLayout.LayoutParams) {
params.gravity = Gravity.TOP
} else if (params is FrameLayout.LayoutParams) {
params.gravity = Gravity.TOP
}
snackbar.view.layoutParams = params
snackbar.animationMode = ANIMATION_MODE_FADE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment