Android Toast message widget example Tutorial

Displaying Toast message in android application on button click.

Toast message is a type of notifier message display quickly on android mobile screen containing a message to its users. Toast message is very important for development purpose because whenever developer needs to show warning message, login successfully message, activity exit message then all those type of message shows using Toast widget. In my this tutorial i am showing a toast message on button click event. So here is the complete step by step tutorial for Android Toast message widget example Tutorial.

android-project-download-code-button

Also read:

How to Add image icon inside Toast message.

How to change toast message position on activity screen.

How to change toast message background with border.

Android Toast message widget example Tutorial.

Code for MainActivity.java file.

 package com.toast_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

 Button ToastShow;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ToastShow = (Button)findViewById(R.id.button1);
 ToastShow.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Toast.makeText(MainActivity.this, "Toast Message on Button Click", Toast.LENGTH_SHORT).show();
 
 }
 });
 
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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"
 tools:context="com.toast_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="CLICK HERE TO DISPLAY TOAST MESSAGE" />

</RelativeLayout>

Screenshot:

Android Toast message widget example Tutorial

Click Here to download Android Toast message widget example Tutorial project.

Leave a Reply

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