How To Display Toast in Android With Example?

Published by Shivam Pandey on

Android Toast Example

Android Toast can be used to display information for a bit of time. A toast contains a message to be exhibit quickly and recede from view after some time.

The android.widget.Toast class is the subclass of java.lang.Object class.

Toast Class

Toast class is used to show notification for a respective delay. After some time it disappears. It doesn’t block the user interaction.

Constant of Toast Class

There are only 2 constants of Toast class which are given below.

 

Method of Toast Class

The widely used methods of Toast class are given below.

toast

Android Toast Example

Toast.makeText(getApplicationContext(),"Hello BrighterBees",Toast.LENGTH_SHORT).show(); 


Another Code

Toast toast=Toast.makeText(getApplicationContext(),"Hello BrighterBees",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();

Here, getApplicationContext() method returns the instance of Context.

Full code of activity class displaying Toast in Android

package example.shivam.com.toast;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

//Displaying Toast with Hello BrighterBees message


Toast.makeText(getApplicationContext(),"Hello BrighterBees",Toast.LENGTH_SHORT).show();
}
}

In case of any query in display toast , you can comment that below or you can connect with the author on 

You can connect with the author on LINKEDIN and TWITTER

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT THIS DEVELOPMENT.

IF YOU WANT TO KNOW ABOUT XML click here.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US