--ADVERTISEMENT--

An android toast displays small message to the users about an operation without user input. Toast is displayed in front of the activity and it remains visible for a short time. Normally, android toast is displayed at the bottom of screen, you can also change display position. You can make custom design for toast using xml layout. Nowadays, most of the android application use Snackbar instead of toast.

In this example, I will show how to display simple android toast in your app.

Android Toast Example - How to Display Toast Message in AndroidRelated:
Android Snackbar Using Design Support Library
Custom Android Toast - How to Create a Custom Toast in Android

How to Display a Toast in Android


To display toast in your application, you have to use makeText() method. Use following toast, one is used to display for long period of time and another is used to display for short period of time. Use following code in onClick() method.
// display toast in long period of time
Toast.makeText(getApplicationContext(),"This is my toast message",Toast.LENGTH_LONG).show();

// display toast in short period of time
Toast.makeText(getApplicationContext(),"This is my toast message",Toast.LENGTH_LONG).show();
Modified code of activity layout file looks like this.
res/layout/activity_main.xml

Modified code of Java Activity looks like this.
src/MainActivity.java

That's all. Now, run your application that looks like this.

Simple Android Toast Example
Android Toast Example

Post a Comment

Powered by Blogger.