How do you display a Toast using Kotlin on Android?

Andrew

In different Kotlin examples for Android I see toast("Some message...") or toastLong("Some long message"). For example:

view.setOnClickListener { toast("Click") }

As I understand, it is an Extension Function for Activity.

How and where do you define this toast() function so that you are able to use it throughout the project?

nhaarman

It can be an extension function for Context:

fun Context.toast(message: CharSequence) = 
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

You can place this anywhere in your project, where exactly is up to you. For example, you can define a file mypackage.util.ContextExtensions.kt and put it there as a top level function.

Whenever you have access to a Context instance, you can import this function and use it:

import mypackage.util.ContextExtensions.toast

fun myFun(context: Context) {
    context.toast("Hello world!")
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to display Toast in Android?

From Dev

How does Android Studio know to display the "Toast created but not shown: did you forget to call show()?" warning?

From Dev

How to display new Toast that overrides old Toast in no time in android

From Dev

How to display new Toast that overrides old Toast in no time in android

From Dev

How do I display a Toast in a setOnClickListener?

From Dev

How to get android Toast Height before display

From Dev

How to Display the computed perimeter using Toast

From Dev

How do you compile Java+Kotlin project using Maven?

From Dev

How do you display a 2 digit NumberPicker in android?

From Dev

How do you use an android tablet as a second display?

From Dev

How do you display markdown value using jinja2?

From Dev

Using AmPieChart in amcharts how do you display the total of the grouped section?

From Dev

Toast is not display after send sms using intent in android

From Dev

How to display toast in AsyncTask

From Dev

How to display a Toast message in the top of the page in my android application

From Dev

Android: can't display a toast

From Dev

How do you start an Android Wear watch face using ADB?

From Java

How do you close/hide the Android soft keyboard using Java?

From Dev

How do you send message using NFC with Xamarin.Android?

From Dev

How do you set the Android Build version (Using Xamarin)?

From Dev

how to display arraylist items in toast?

From Dev

How to display newline / CR in toast()?

From Dev

How to display Toast from AlertDialog?

From Dev

how to display arraylist items in toast?

From Dev

How to cancel the toast in android?

From Dev

How do i show toast and title in new changed language in android

From Dev

How do i show toast and title in new changed language in android

From Dev

How to display rating value(Integer) from an API (Google api) using ratings bar in Android (preferably using Kotlin)?

From Dev

How do you detect a Retina Display in Java?

Related Related

  1. 1

    How to display Toast in Android?

  2. 2

    How does Android Studio know to display the "Toast created but not shown: did you forget to call show()?" warning?

  3. 3

    How to display new Toast that overrides old Toast in no time in android

  4. 4

    How to display new Toast that overrides old Toast in no time in android

  5. 5

    How do I display a Toast in a setOnClickListener?

  6. 6

    How to get android Toast Height before display

  7. 7

    How to Display the computed perimeter using Toast

  8. 8

    How do you compile Java+Kotlin project using Maven?

  9. 9

    How do you display a 2 digit NumberPicker in android?

  10. 10

    How do you use an android tablet as a second display?

  11. 11

    How do you display markdown value using jinja2?

  12. 12

    Using AmPieChart in amcharts how do you display the total of the grouped section?

  13. 13

    Toast is not display after send sms using intent in android

  14. 14

    How to display toast in AsyncTask

  15. 15

    How to display a Toast message in the top of the page in my android application

  16. 16

    Android: can't display a toast

  17. 17

    How do you start an Android Wear watch face using ADB?

  18. 18

    How do you close/hide the Android soft keyboard using Java?

  19. 19

    How do you send message using NFC with Xamarin.Android?

  20. 20

    How do you set the Android Build version (Using Xamarin)?

  21. 21

    how to display arraylist items in toast?

  22. 22

    How to display newline / CR in toast()?

  23. 23

    How to display Toast from AlertDialog?

  24. 24

    how to display arraylist items in toast?

  25. 25

    How to cancel the toast in android?

  26. 26

    How do i show toast and title in new changed language in android

  27. 27

    How do i show toast and title in new changed language in android

  28. 28

    How to display rating value(Integer) from an API (Google api) using ratings bar in Android (preferably using Kotlin)?

  29. 29

    How do you detect a Retina Display in Java?

HotTag

Archive