Tag Archives: messages

Using Toasts to display messages in Android

A Toast is a small message that is temporarily displayed at the bottom of a screen in Android apps and is usually used to notify the user that something has occurred or changed, or to confirm an action that the user took such as saving a file or sending a message. The example app below shows a Toast that gives a confirmation to the user that they clicked a button. The Toast will appear when the button is clicked and then disappear a few seconds later.

image1

Step 1

For this example, we will display a Toast when a button is clicked. Add a button to your app’s screen. In this example, we are using the Main.axml screen and the default button.

image2

Step 2

When the button is clicked it will display a toast. You need to define the button in your code and create a click event for it. You can use the existing button and click event that usually exists by default when you create a new Android app, if you wish.

The following line needs to be added inside the click event for the button to make the Toast appear:

Toast.MakeText(this, "You clicked the button", ToastLength.Long).Show();

The complete code should look something like this:

image3
Click the image to see a larger version.

The string message inside the quotes could also be replaced with a variable’s value, or you could also display a variable’s value joined to a string. The Toast can be used for any event such as a click event, when an option is selected or changed, after a certain time period, when something is received, or even just when the app loads.

Go and test your app and it should look something like this when the Toast appears after clicking the button.

image1