How to display toast message from another file?

I can place the following code inside the MainActivity and get the correct toast message

Toast.makeText(this@MainActivity, “This works”, Toast.LENGTH_LONG).show()

However, if I place the same line in another file that contains a class, I get an error that @MainActivity is unresolved. I think I need to make MainActivity global, but I am not sure how to set that. Any suggestions would be appreciated.

First of all please read the documentation and concider what does this@MainActivity expression mean in Kotlin. It would be obvious to you after that.

1 Like