Android Toast Message Part — 1
What’s up guys, welcome back to the Android App Development Tutorial. In this tutorial, I will talk about Toast in Android.
Now, what is a Toast? All the stuff that you are able to see on your screen is a Toast message. When you send an email, it shows “Email was sent successfully”. So, a small popup that appears on your smartphone when your phone is connected to WiFi, when you set an alarm, or when there is some error with an application, that stuff is known as Toast in Android.
Also, from the developer’s point of view, the Toast is very important. In case you want to debug any variable or module of your application, in that case, Toast will really help you to check whether the variable or the method is getting the desired value or not.
Now, what is the syntax of writing a Toast in Android? For that, you have to initialize a Toast object of the Toast class. Write:
Toast toast = Toast.makeText(getApplicationContext(), "Email Sent!", Toast.LENGTH_LONG);toast.show();This function “makeText” requires three parameters. The first is a context, the second is the text that you want to print on your screen, and the third is the duration constant till what time you want the Toast to appear on the user’s phone.
This context means the context of the activity from which you are showing the Toast in the smartphone. This context can be written as “getApplicationContext()” or simply “MainActivity.this”. From this activity, you are calling this Toast message.
This text is actually the CharSequence or the string you want to display. It should be in the string format.
This duration can be written in two parameters. The first can be Toast.LENGTH_SHORT and the second is Toast.LENGTH_LONG. The first one displays the Toast message for a very short duration and the second one displays the Toast message for a longer duration.
Get Birbal Jaat’s stories in your inbox
Join Medium for free to get updates from this writer.
So, these two lines of code complete our syntax of Toast in Android. Now, let’s jump onto the Android Studio and show you the code.
Now, here in the Android Studio, I have created an application “Toast_Message”. Now, I’m having the activity MainActivity and the XML file of the activity_main is as such. I have a button. On click of the button, we have to pop up a Toast message.
Now, here I am having the button click handler or the method. When you click on the button, this method will be executed. Now, let’s write a Toast message:
Toast toast = Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG);toast.show();Now, save it and run your application. Bingo! Our app is running right in front of us.
Now, let’s click on the button. Show Toast! Yay! Email Sent! Click on it again. Click on it again. It will appear one by one. It will be queued up.
Thanks for reading guys! Catch you guys in the next tutorial. And please comment on my tutorial, share, like, and clap too. Thank you and have a good day!
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.










