Looking For Anything Specific?

[Tutorial] How to Show Toast Message In Android From Activity with Kotlin -MAD

In this tutorial we will learn step-by-step tutorial on how to make your first android application that will show a toast message, A toast message is a simple alert type message that will show for a small/ or a little bit of time as we mention in the code. A toast message is one of the important and useful things to learn in android application development as it gives you some insight into the android application development itself and will give you the possibility to explain something to the user without interfering with the user experience. A toast message in an android application is very common so learning that also a good thing. Below is a screenshot showing a "Hello World" text as a toast message in a simple application that's what we are going to build in this tutorial.

kotlin android app tutorial with sample code
Final Result


And toast messages in android are also used to debug your application in case of errors, and with which you can simply output the result so that you can check before updating the view, this is one way but the best way to debug an android application is through logs. We will learn ways to debug an android application in the future as we get along with some serious codes

What is Toast Message in Android?

We covered this part a little in the intro yet here, is a vast explanation of what is a toast message in android. Toast messages are a simple display block that will give the user feedback about the action that the user made, sometimes a result of an action completed so that the user can know, or we can use it for simple debugging. Toast messages are built in android classes so it is very easy to use and helpful, but in the modern application, it was replaced by Snackbar which is a colorful alternative to android toast messages. We will learn about android snack bar in later tutorials

Step by step tutorial to show toast message in android with kotlin

As mentioned in our early post we will only use kotlin for android application development
  • Start Android Studio
  • Select Empty Activity
  • Enter Project Name and set the package name 
  • And choose the language as Kotlin
  • And Click Finish and wait for Gradle to complete the build
Make sure you have an active internet connection and your firewall isn't blocking android studio. Once the build was successful you will see the project structure as below.

In this tutorial, we only covered how to show a toast message from an activity, but it is a little trickier from fragment to show a toast message you can learn about that in a 

You might be interested in learning:

Now head over to MainActivity.kt file, and you can simply copy-paste the below code, but we suggest typing the code by yourself because it is the best practice, and in the MainActivity.kt after the pre-generated code type the code below

Below is the code for making a toast message

Toast.makeText(applicationContext,"Welcome Form Kotlin",Toast.LENGTH_LONG).show()

In the above code, we call the Toast class which is under android.widget.Toast and in that class we call makeText() method which takes three parameters and we finally call the show() method to display the toast message in android, If you put the above code inside onCreate you can see the toast message whenever  you start the activity more likely switching between screens or changing orientations

context: Context: it requires context from where the toast is started.
message: String: a string that is going to be displayed
duration: int: and the duration how long it will be displayed
.show(): is a function to show the toast messages

For the duration parameter, we use like

So by giving these three parameters, we can simply make a toast message in android, you can see the output below if this is what you're looking in your screen well done you're an android developer now.


But wait for android toast message is not showing? , well you must have missed something in my case I mostly miss the last method .show(), without this, your toast message will not show yeah but it will still compile and run but will not display, but if you miss out on any parameter it will throw you an error in compile time. This is an easy way to display a toast message in android.

And to take it to the next level and to have some fun coding around we add some extra things like a text input box aka EditText in android and a button, yeah android it is also a button and we implement a little more code to toast the text from the input we give, which is pretty simple.

Now you can use the above Toast.makeText().show() syntax to create a toast message with the given input text.

Complete Code:

val editext = findViewById<EditText>(R.id.editText) //get edittext using id

val button = findViewById<Button>(R.id.toastButton) //get button using id

button.setOnClickListener { //setting listener function to button , using lambda expression , 

called when button clicked    

val message2 = editext.text.toString() //get input from edittext and convert to text

    Toast.makeText(applicationContext, message2, Toast.LENGTH_SHORT).show() //display toast with input}

Java vs Kotlin difference :

Kotlin doesn't have a new keyword, so here we use the lambda function on button.onclicklistener. This decreased the no. of lines and pretty much easy to learn and use no messy code.

You can view the complete code from my GitHub page: BasicKotlinApp
or Download the complete project at here GDrive Link

And if you're wondering how can I display a toast message in android from a fragment ?, yeah that needs a little effort and need somethings to take care of, why is that so? because fragments are different dorm activities and they don't have the context of their own which is the first parameter that is required by Toast.makeText() method but don't worry head over to this page to learn about fragment and display a toast message in it.
Congratulations!!! You have don't it now you can head over to the next tutorial to make buttons and displays message on click, or you can take a break and come back don't forget to bookmark and share this blog its a new one so we need more supporters

In any case, if you find any error, mistake or misleading information feel free to drop a mail at our contact page, those things will be taken care of.

Don't be a stranger!!! Comment your thoughts, and don't forget to share.

!!!🥰Sharing is Caring🥰!!!


keywords: