Last active
February 13, 2021 14:59
-
-
Save BVantur/1dbe2d6720057d273f42757af9389427 to your computer and use it in GitHub Desktop.
ToastUtils
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
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