Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the KTX toast extension function?

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?

like image 931
Shreck Ye Avatar asked Jan 11 '19 07:01

Shreck Ye


2 Answers

Looks like Context.toast extension was removed from ktx lib https://github.com/android/android-ktx/issues/143#issuecomment-417891391

like image 114
deviant Avatar answered Oct 17 '22 09:10

deviant


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()
    }

like image 2
wang simon Avatar answered Oct 17 '22 11:10

wang simon