Published on Sept. 14, 2021
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 is used to show notification for a respective delay. After some time it disappears. It doesn’t block the user interaction.
There are only 2 constants of the Toast class which are given below.
public static final int LENGTH_LONG //displays view for the long duration of time
public static final int LENGTH_SHORT //displays view for the short duration of time
Creating A Toast
Here is an Android Toast example:
Code Format 1
Toast.makeText(getApplicationContext(),"Hello BrighterBees",Toast.LENGTH_SHORT).show();
Code Format 2
Toast toast = Toast.makeText(getApplicationContext(), "Hello From BrighterBees", Toast.LENGTH_SHORT); toast.setMargin(50,50);
toast.show();
Here, getApplicationContext() method returns the instance of Context.
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);
Toast.makeText(getApplicationContext(),"Hello BrighterBees",Toast.LENGTH_SHORT).show();
}
}
In this article, You get to know how to display toast in Android with suitable examples and proper code.
···