Android Toast Example

Hello everyone ,

Our next Example in Basic Android Examples is Basic Toast Example . In this post we will create a Button on click of which you ll be able to see a notification on Mobile screen which is known as Toast .

So basically , what is a toast ?

It is actually like an alert message. android.widget.Toast class used to create toast alert message.

Uses :

1. Toast alert is a notification message that display for certain amount of time, and automtaically fades out after set time.

2. Use it to show alert message to user.

3. can use it for debugging your application. show alert from background sevice,boadcast reciever,getting data from server...etc.

Let us start with the code :

Step 1: Create a New Application

Let us first create a new Application/Project . I named it as Toast Example.
Android Example
Step 3:Activity  

Now add a new buuton either by dragging in activity_toast.xml or by writing the code . I changed the text of button as Toast Example .
Android Toast Example

Step 3: Add a New Button 
For Activity java class first we need to get the Button id using findIdbyView method and then we override the onclicklistener method onClick  with which a Toast with the String inside it and length defined will be shown on the mobile screen .


import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ToastActivity extends AppCompatActivity {

    private Button toastExample;

    protected void onCreate(Bundle savedInstanceState) {

        toastExample = (Button) findViewById(;

        // Button click listner
        toastExample.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Context context = getApplicationContext();

               // Toast toast = new Toast(context);

                        "Hello Toast", Toast.LENGTH_LONG)



Step 4: Output 

Android Example

Post a Comment