Last active
April 26, 2017 16:11
-
-
Save brescia123/679f375d9147787f2bf4aa6e38e7b9b7 to your computer and use it in GitHub Desktop.
kUtils - Snackbar
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
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