Reputation: 769
I am not able to show a toast on a fragment. It shows the error as in the screenshot. I am using kotlin
. Please advice.
Error message
Upvotes: 0
Views: 678
Reputation: 869
Use Fragment's activity
. You can use extension function to easily produce toasts in fragments:
fun Fragment.makeToast(text: String,duration: Int = Toast.LENGTH_LONG) {
activity?.let {
Toast.makeText(it, text, duration).show()
}
}
Now in the Fragment you can just use it like:
class LoginFragment : Fragment() {
//...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//...
makeToast("lets start")
Upvotes: 1