ToastExample

In this tutorial we will explain how to work with Android Toast with example. The example below demonstrates the usages of simple and customized toast in Android.
1. Toast is a solution for android developer when required to notify user about an operation without expecting any user input.
2.This provides a small popup that displays for a small period and fades out automatically after timeout. Sometimes developers use this for debugging.
3. For example, some of the app shows “Press back once to Exit” message when pressed back button in home page. Another real-time example is Gmail app, It shows a Toast, when a mail message is saved to draft.

1. MainActivity.java

package com.tutorialsee.toastexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.button1);
Button click2 = (Button)findViewById(R.id.button2);

click.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.custom_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});

click2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(),"Either Pincode is Incorrect or is Empty", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 280);
toast.show();
}
});
}
}

2. activity_main.xml

<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:background="#FAFAFA"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="92dp"
        android:background="#009688"
        android:text="@string/textss"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/texts"
        android:textColor="#ffffff" />

</RelativeLayout>

3. toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#B2DFDB"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@drawable/ic_launcher"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/text"
        android:gravity="center"
        android:textColor="@android:color/black" />

</LinearLayout>