Skip to content

Instantly share code, notes, and snippets.

@BVantur
Last active February 13, 2021 14:59
Show Gist options
  • Save BVantur/1dbe2d6720057d273f42757af9389427 to your computer and use it in GitHub Desktop.
Save BVantur/1dbe2d6720057d273f42757af9389427 to your computer and use it in GitHub Desktop.
ToastUtils
object ToastUtils {
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 toastView = LayoutCustomToastBinding.inflate(activity.layoutInflater).apply {
root.setBackgroundColor(backgroundColor)
message = textMessage
}
Toast(activity)
.apply {
setGravity(
Gravity.TOP or Gravity.CENTER_HORIZONTAL or Gravity.FILL_HORIZONTAL,
0,
0
)
view = toastView.root
duration = Toast.LENGTH_SHORT
}.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment