Android program to display a Toast Message.

Android program to display a Toast Message

In Android, Toast is used to display short messages to the user which automatically disappears after a certain period of time. If you have ever uninstalled an android app and once the installation is done you get a small message “Uninstalled finished” (similar to this)which will disappear after a short time. Here I will be showing you how to display a Toast message on click of a button. But before I move on to the program the structure of a Toast message is Toast.makeText(Context, text, duration(Short/Long)) and to display the toast you use the show() method.

Here the code is to display a Toast message on click of a button. First, create a button in activity_main.xml file, you can either create it using GUI or by coding it. Once the button is created bind it with the button object in the MainActivity.java file. Once this is completed then use the setOnClickListener method in Android and then display the Toast message inside this method(as you want to display the toast message when a user clicks on the button).

Steps to create the application:- 

  •  Open Android Studio and create a new Android application and name it as “Toast” and company domain as codedost so your package will be automatically set.
  • Open an Empty activity and name it as MainActivity.
  • Copy the content of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

MainActivity.java

package codedost.toast;

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

public class MainActivity extends AppCompatActivity {

    Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.toast_button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"You just Toasted me!!!",Toast.LENGTH_LONG).show();
                /*Another way to display a Toast message
                    Toast t=Toast.makeText(MainActivity.this,"You just Toasted me!!!",Toast.LENGTH_LONG);
                    t.show();
                */
            }
        });
    }

}

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context="codedost.toast.MainActivity">

    <Button
        android:id="@+id/toast_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast me!"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
    />

</RelativeLayout>

Output

Share Me!