Android Toast Message

If you want to show small message to user without any input operation, then what will you do, explain in brief.
Or
Explain Toast class with example.

  • Toast class is used to show small message to user without expecting any user input. It provides a popup message that displays for a small period of time and disappears automatically after timeout. Toast notification, bydefault appears near the bottom of the screen, But you can change this position by using setGravity() metod.
  • Toast class is available in android.widget.Toast package, so you must import this package in your application.
  • Lets take an example. Take a button and change its ID, I have taken ID as btnShow. Create a method that will be called when user clicks on the button. I have created showToast() method.
Following will be the content of res/layout/content_main.xml file:
<?xml version = "1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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"
android:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
tools:context = "com.example.itwin.toastdemo.MainActivity"
tools:showIn = "@layout/activity_main">

<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Show Message"
android:textSize = "30dp"
android:id = "@+id/btnShow"
android:layout_marginTop = "72dp"
android:onClick = "showToast" // It will call showTost() method
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
</RelativeLayout>

Following is the content of the modified MainActivity.java file
public class MainActivity extends AppCompatActivity
{
     Button btn;
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btn = (Button)findViewById(R.id.btnShow);
     }
     public void showToast(View v)
     {
          Toast toast = Toast.makeText(getApplicationContext(), "Welcome to CareerRide", Toast.LENGTH_SHORT);
          toast.show();
     }
}

Run the application and click on the show message button, you will see the output given below.
Toast message

Now let us discuss the makeText() method of toast class. It is static method of Toast class and takes three parameters.
  • First parameter is Context object which is obtained by calling getApplicationContext().
  • The second parameter is the string to be displayed in the message.
  • The third parameter is the time duration the Toast is to be displayed. The Toast class contains two predefined constants for duration that can be used

    Toast.LENGTH_SHORT: Show the notification for a short period of time.
    Toast.LENGTH_LONG.: Show the notification for a long period of time.