Skip to content

Instantly share code, notes, and snippets.

@brescia123
Last active April 26, 2017 16:11
Show Gist options
  • Save brescia123/679f375d9147787f2bf4aa6e38e7b9b7 to your computer and use it in GitHub Desktop.
Save brescia123/679f375d9147787f2bf4aa6e38e7b9b7 to your computer and use it in GitHub Desktop.
kUtils - Snackbar
fun View.createSnackbar(text: String,
duration: Int = Snackbar.LENGTH_SHORT,
actionLabel: String = "Action",
dismissOnAction: Boolean = true,
action: ((View) -> Unit)? = null): Snackbar {
val snackbar = Snackbar.make(this, text, duration)
action?.let {
snackbar.setAction(actionLabel, {
action(it)
if (dismissOnAction) snackbar.dismiss()
})
}
return snackbar
}
fun View.createSnackbar(@StringRes resId: Int,
duration: Int = Snackbar.LENGTH_SHORT,
actionLabel: String = "Action",
dismissOnAction: Boolean = true,
action: ((View) -> Unit)? = null): Snackbar {
val text = context.getText(resId).toString()
return createSnackbar(text, duration, actionLabel, dismissOnAction, action)
}
fun View.showSnackbar(text: String,
duration: Int = Snackbar.LENGTH_SHORT,
actionLabel: String = "Action",
dismissOnAction: Boolean = true,
action: ((View) -> Unit)? = null) {
createSnackbar(text, duration, actionLabel, dismissOnAction, action).show()
}
fun View.showSnackbar(@StringRes resId: Int,
duration: Int = Snackbar.LENGTH_SHORT,
actionLabel: String = "Action",
dismissOnAction: Boolean = true,
action: ((View) -> Unit)? = null) {
createSnackbar(resId, duration, actionLabel, dismissOnAction, action).show()
}
/** Convenient method that shows or hides a Snackbar */
fun Snackbar.showOrHide(show: Boolean) {
if (show) show() else dismiss()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment