Android Toast Example

By | October 11, 2012
Android Toast Example
Toasts are transient Dialog boxes that remain visible for only a few seconds before fading out .
  • You can make toast by using Toast class .  
  • The Toast class includes a static makeText method that creates a standard Toast display window.
      pass the following parameter to this methods 
             1) Application Context . 
             2)  The text message to display
             3) The length of time to display it (LENGTH_SHORTor LENGTH_LONG) . 
     
  • Once a Toast has been created,display it by calling show.

     Customized Toast 

     You can also customize the toast  by setting its display position and assigning it alternative Views or layouts.

     Lets see an Example 
    Project Detail

    Project Name ToastDemo
    Package com.pavan.toastdemo
    Minimum SDK API 8
    Target SDK API 17
    Theme Holo Light with Dark Action Bar

    1. XML Layout

       file:-activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical" >

    <Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Simple Toast" />

    <Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Customized Toast" />

    </LinearLayout>

    Create An Xml for custom toast message

       file:-custom_toast.xml

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

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:src="@drawable/ic_launcher" />

    <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text=""
    android:textColor="#ffffff"
    android:textSize="16dp"/>
    </LinearLayout>

    2. Activity

    file:-MainActivity.java

    package com.pavan.toastdemo;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;

    import android.widget.TextView;
    import android.widget.Toast;
    import android.app.Activity;

    public class MainActivity extends Activity implements OnClickListener {

    Button sim_toast_but, cust_toast_but;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sim_toast_but = (Button) findViewById(R.id.button1);
    cust_toast_but = (Button) findViewById(R.id.button2);

    sim_toast_but.setOnClickListener(this);
    cust_toast_but.setOnClickListener(this);
    }

    public void onClick(View v) {

    switch (v.getId()) {
    case R.id.button1:
    Toast.makeText(this, "Displaying Simple Toast", Toast.LENGTH_SHORT)
    .show();
    break;

    case R.id.button2:
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,
    (ViewGroup) findViewById(R.id.layoutid));

    ImageView iv = (ImageView) layout.findViewById(R.id.imageView1);
    TextView tv = (TextView) layout.findViewById(R.id.textView1);

    iv.setImageResource(R.drawable.ic_launcher);
    tv.setText("Displaying custom toast");

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.BOTTOM, 0, 50);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
    break;
    }
    }
    }

    3. RUN

    Android Simple Toast Message


    Android Custom Toast