Menu
Toast in Android Example
Toast in android is used to display a message on screen for a small period of time. Toast fades automatically after small time. Toast.LENGTH_SHORT and Toast.LENGTH_LONG are two constants to specify duration of Toast.
// Syntax to display Toast
Toast.makeText(context, "message", duration).show();
context – This is our application context.
message – Type a message here to display on screen.
duration – Time of toast to simply on screen.
show() – This method is used to display the toast.
// Example
Toast.makeText(MainActivity.this,"Toast Example message",Toast.LENGTH_LONG).show();
// MainActivity.java
package com.androidaura.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(context, text, duration).show();
Toast.makeText(MainActivity.this, "Toast is Displayed", Toast.LENGTH_SHORT).show();
}
}