What is Toast

A Toast is a widget that we can use for display the little message on to the screen. A Toast display the message for a period of time.

Normally it display the message at bottom of the screen you can change the position of Toast message by using the setGravity() method.


Toast:.

In this section i am going to show you how to Toast display the message with practical example.

First open the android studio and create a project and give the name of your project like ToastDemo.

The out put of the program screen shots are as follows.

Smiley face Smiley face

Drag and drop a Button. Open the activity_main.xml file and code look like this.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.niitpc.myapplication.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="136dp"
android:layout_marginStart="136dp"
android:layout_marginTop="205dp"
android:text="Toast Message" />
</RelativeLayout>

Open the MainActivity.java file and write code look like this.

When you click on Button it will display the message welcome to AndroidATPoint.


package com.example.niitpc.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;//you must add this line other wise it gettting error.
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      

    }
    public void display(View v)
    {
        Toast.makeText(getApplicationContext(),"Welcome to AndroidATPoint",Toast.LENGTH_SHORT).show();

    }

}

       

You do not forget to set the propery of Button android:onClick="display" other wise it does not work at the click of Button.

First of all i have created a method display().Within this method i am using the Toast method makeText() and this method take three argument:

1.First argument is getApplicationContext . The first argument you need to pass the context of the current application you can also write MaintActivity.this if you are in MainActivity.java file

2.The second argument "Welcome to AndroidATPoint".This is the message you want to display on the screen.

3.The third argument Toast.LENGTH_SHORT.It decide the appreance time period of message.There are two types of Constant.

public static final int LENGTH_LONG.displays message for the long duration of time.

public static final int LENGTH_SHORT.displays message for the short duration of time.


Toast Positioning:

You can change the positioning of Toast message on the screen by using the setGravity() method. Here is a Toast setGravity() example:

Toast.setGravity(Gravity.CENTER, 0, 0);

You can use the following constants in the Gravity class to specify the overall position:

Toast of Android

what is Toast for video click here

Smiley face                     Smiley face