Toast Message in Android Studio


Toast Message in Android Studio:- in this article, I am going to show you how to use toast in your application. In this article I am covering two examples one is simple and in the second I am using toast message inside if condition.

Toast Message in Android Studio:

Toast message in android is a very good reminder method provided by the Android system. You can use it in the program. The information is notified to the user. The information will disappear automatically after a period of time and will not occupy any screen space. now we just try to use Toast message in android activities.

Example: Create a simple toast message in android

First, you need to define a trigger point to pop up a Toast message in android. For that simply we need a button on the interface, when the button is pressed, a Toast message will be shown.

Open android studio, create new project, and Write the below code in activity_main.xml file:

Then write the below code in file:

program explanation:

In the activity, you can get the elements defined in the layout file through the findViewById() method.

Here i pass to get an instance of the button. This value is just passed the android:id attribute in activity_main.xml Specified. What the findViewById() method returns is a View object, we need to downcast it to a Button Object. After getting the button instance, i register a listener for the button by calling the setOnClickListener() method When the button is clicked, the onClick() method in the listener will be executed. Therefore, the function of popping up Toast message is of course Written in the onClick() method. The usage of Toast message in android  is very simple. Then I Create a Toast object through the static method makeText(), and then call show() to display the Toast message. It should be noted that the makeText() method needs to pass in three parameters. The first parameter is Context, which is the context required by Toast. Since the activity itself is a Context object, Here you can directly pass in MainActivity.this. The second parameter is the text content displayed by Toast message, and the third parameter For the displayed duration, there are two built-in constants to choose from Toast.LENGTH_SHORT and Toast.LENGTH_LONG.

Now run the program and click the button, you will show the following result.

Toast message in android

When you clicked the button it will show a toast message button clicked

Toast message in android

Example: How to use toast message inside Condition in Android studio:

in this example, I am going to show you how to use a toast message inside condition so what is a toast? a toast is basically a feedback message that is shown to the user screen based on an action.

let’s go to android studio and build the application.

first, create a new project

Toast message in android

and then select empty activity and click the next button.

Toast message in android

The give it name in my case I give it “ToastMessageDemo”, and then click on finish

Toast message in android

ok build is finished and load successfully

Toast message in android

In this app, I am using four edit Text and one button, so open the activity_main.xml file and modify the code as follows:

First of all, I change the layout from Constraintlayout to Relativelayout. The code is very simple as you can see I used four edit text fields (First name, last Name, phone number, address). and one button.

Toast message in android

Now modify the code in as follows:

Program Explanation:

First of all I write the code for the buttons and the edit text  fields so we have the edit text firstname,  lastname, phoneno,and address, and then a button i call it submit button

Then I get the elements defined in the layout file through the findViewById() method

then I set the event onclicklistener on the submit button. inside onclick method I do some logic there i need to check for four conditions, when the user have felt the fields successfully It will show a toast message now if the user have forgot to fill either one of these for example if he fill in the name but forget to fill in the address  then It will show the else statement toast message

the final output of Toast Message in Android Studio:

Toast message in android

When the field is empty it will show a toast message please don’t leave any field empty

Toast message in android

When all the fields are filled it will show a toast message detail submitted

Toast message in android


Related Articles

Leave a Reply

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

Back to top button