How to make a toast message in android studio

How to display toast in android

Toast shortcut android studio

What is toast?

Toast is nothing but displaying a short message to a particular timer.

To display toast we will import toast widget, in android studio automatically import toast widget when use toast to display message.

Here, we’ll display a simple toast message. You can put custom toast with design.

When use Toast?

You can use toast when you need to displaying some method for example buttons when users click on buttons then particular display message short or long duration then we’ll use toast.

How to use toast?

You use toast simply toast class to display a simple toast message.


Toast makeText (Context context, CharSequence text, in duration) display message method


                Toast.makeText ( MainActivity.this, “this is second toast”, Toast.LENGTH_LONG ).show ();

Toast message Example

Step 1

Open android studio and create a new project.

Step 2

Here given default class and activity_main.xml file.

Step 3

We will simply use a button in XML to display the message.  using the click-on button will display the short or long message, it automatically disappears after a short or long duration.

Toast message java code
Toast message code

So first we’ll create a button in the activity_main.xml file

below code activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

Toast message xml file
Toast message XML file

Step 4

Now, open class and type below the java code. file

package com.example.toast;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
Button toastdisplay;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        toastdisplay=findViewById ( );
        toastdisplay.setOnClickListener ( new View.OnClickListener () {
            public void onClick(View v) {
               // Toast.makeText ( getApplicationContext (),"this is toast",Toast.LENGTH_SHORT ).show ();

                Toast.makeText ( MainActivity.this, "this is second toast", Toast.LENGTH_LONG ).show ();

        } );

Click on the run button and run your application.

display toast message
display toast message when clicking on a button

Now, we’ll discuss some methods and classes.

Here, we’ll make a simple toast message in android studio.

Toast.LENGTH_LOGN: it contains a duration of time.

LENGTH_LOGN means it displays some long duration of time.

Toast.LENGTH_SHORT: it contains the duration of time. LENGTH_SHORT means displaying messages too short a time after disappear.

What is a shortcut to toast generation?

In Android Studio, you don’t need to type complete code just type Toast and press enter, automatically the code will be completed. All you have to do is write a text message about what you want to do.

Leave a Reply