How to use Toast in Android part 1

Abi Farhan
2 min readJun 7, 2021

--

Hello everyone, this is my first post about Android Development. In this session, I want to share my knowledge about Toast in Android Development.

What is Toast?

Toast is a simple message that we use to show the message to the user to tell the user the interaction has been done through a small pop-up with the message.

Example of Toast

Then, why we use Toast? there is some reason for using the Toast:

1. To make sure our program runs as we expected, for example, you want to make sure the button works if you clicked, then you can use Toast to give a simple message if the button works when clicked.

2. To give some information to the user, for example, you make the upload data into the online database like Cloud Firestore, then when the upload is success the program will tell the user that success uploads the data into the database.

3. To make sure the data you send between two activities or fragments is a success, the example similar in point 2.

4. Etc.

Then, how we make Toast in Android. Let’s get started on it.
Let say you already make one project with one activity name MainActivity.kt. use this code for the activity:

Visit the link here

before we go to the MainActivity.kt, we need to make sure ViewBinding already provide in our project, if not yet, then you can add this code to build.gradle(:app) file

Visit the link here

you can also use findViewById, kotlin-extension, etc to initialization the layout item. Alright then we go to MainActivity.kt.

add this line of code in MainActivity.kt :

Visit the link here

To use the Toast we have to input 3 parameters :

  1. Context
  2. The message we want to convey
  3. The duration of the message before it’s gone (the Toast.LENGTH_SHORT is default by Android Studio that the message will appear just a moment)

Then let’s run the app, the app will show like this:

The Toast show up

Alright, this is for these sessions, I hope you enjoy it. If there is a thing you don’t understand or something is confusing in my explanation feel free to ask in the comment box. I will be happy to share with you.

#AndroidDevelopment#Toast#Android

--

--

Abi Farhan
Abi Farhan

Written by Abi Farhan

Sharing About Software Engineering, System Design, Test Drivent Development, and Data Structure and Algorithm.

No responses yet