Kotlin – How to Show a Toast in Android

🧠 Explanation:

  • Toast.makeText() is used to display short messages to the user.
  • The first parameter (this) is the Context β€” in an Activity, just use this; in a Fragment, use requireContext() or context.
  • The second parameter is the message string to display.
  • The third parameter is the duration:
  • Toast.LENGTH_SHORT (about 2 seconds)
  • Toast.LENGTH_LONG (about 3.5 seconds)

πŸ“Œ Example Usage in Activity:

βœ… Tips

  • You can also use Toast(requireContext(), "Message", Toast.LENGTH_SHORT) in Fragments.
  • For custom Toasts, you can inflate your own layout using LayoutInflater.

1 thought on “Kotlin – How to Show a Toast in Android”

  1. Pingback: 🌟 Welcome to 7SoftSky Developer – Our Mission, Vision & Passion - 7SoftSky Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top