I have learned from this article Even Sweeter Android development with Android KTX (https://www.kotlindevelopment.com/even-sweeter-android-ktx-kotlin/) that Android toast can be simplified using KTX from
Toast.makeText(context, R.string.toast_message, Toast.LENGTH_SHORT).show()
to
toast(R.string.toast_message)
I wanted to try it in my project but I couldn't find it in androidx.core:core-ktx:1.0.0
. So in which dependency is this extension function?
Looks like Context.toast extension was removed from ktx lib https://github.com/android/android-ktx/issues/143#issuecomment-417891391
You can add a method extension to implement, as far as I know, there is no ready-made.
fun Context.toast(message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, this.resources.getText(resId), duration).show()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With