Android custom toast example

In this post, we will discuss about how to create a custom toast message in Android application. We usually see toast as a simple text message which pops up on the screen.

But we can create custom toast with images and more style attributes like padding, gravity, etc.You can create custom toast message shown below at the end of the post:

What is Toast?

A toast is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user’s current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events. Read more at Android developer site

What are we going to develop?

We are going to develop a simple android activity which will have one button at the center of the screen. On clicking the button, custom toast message will be appeared.
[pgsubscribe]

Quick Links

Project Structure

  • Create new android project [File >> New >> Android Project] with project name CustomToastExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Layout creation:
We need to create two layouts:

  1. Layout of the application – main.xml
  2. Layout of the custom toast message – toast_custom_layout.xml
Application layout – main.xml:
 
Open main.xml under /res/layout and add replace it with the below XML.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >

    <Button
        android1:id="@+id/button1"
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content"
        android1:layout_gravity="center"
        android1:text="Show custom toast" />

</LinearLayout>

Custom toast layout –  toast_custom_layout.xml:

Create new layout XML under /res/layout with name toast_custom_layout.xml and the below XML.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android1:id="@+id/imageView1"
        android1:layout_width="wrap_content"
        android1:layout_height="match_parent"
        android1:src="@drawable/red_heart" />

    <TextView
        android1:id="@+id/textView1"
        android1:layout_width="wrap_content"
        android1:layout_height="match_parent"
        android1:text="This is custom Toast message"
        android1:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

We need to inflate the custom toast xml into Toast view:

Following snippet does the job of creating and showing custom toast.

//Retrieve the layout inflator
LayoutInflater inflater = getLayoutInflater();
//Assign the custom layout to view
//Parameter 1 - Custom layout XML
//Parameter 2 - Custom layout ID present in linearlayout tag of XML
View layout = inflater.inflate(R.layout.toast_custom_layout,
			(ViewGroup) findViewById(R.id.toast_layout_root));
//Return the application context
Toast toast = new Toast(getApplicationContext());
//Set toast gravity to bottom
toast.setGravity(Gravity.BOTTOM, 0, 0);
//Set toast duration
toast.setDuration(Toast.LENGTH_LONG);
//Set the custom layout to Toast
toast.setView(layout);
//Display toast
toast.show();

CustomToastExampleActivity.java
Here is the Activity code of this application.

package com.prgguru.android;

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.widget.Button;
import android.widget.Toast;

public class CustomToastExampleActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
                //Find the button
		Button btn = (Button) findViewById(R.id.button1);
                //Create onclick listener class
		btn.setOnClickListener(new View.OnClickListener() {
                        //When button is clicked, toast will be showed
			public void onClick(View v) {
				LayoutInflater inflater = getLayoutInflater();
				View layout = inflater.inflate(R.layout.toast_custom_layout,
						(ViewGroup) findViewById(R.id.toast_layout_root));
				Toast toast = new Toast(getApplicationContext());
				toast.setGravity(Gravity.BOTTOM, 0, 0);
				toast.setDuration(Toast.LENGTH_LONG);
				toast.setView(layout);
				toast.show();
			}
		});
	}
}

[pglinkadssmall1]

Demo

Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:

 

Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

Reference:

Android Toast

I hope you enjoyed the post!! :)

Suggested posts for further reading


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
  • ftxbird

    Nice toast ?

  • XYZ

    nice

  • HI,

    It is very useful for me to understand how to write the code for toast messages. Is it possible to remove the toast message from bar or any other settings in android?

  • ornay

    why are you using two different namespaces? android1 & android?

    • Hi, That doesn’t matter. You just use single namespace.

  • good one. we can see other simple toast example in

    http://articlesforprogramming.blogspot.in/2013/06/toast-in-android.html

  • Coder

    How do I change the text code dynamically ? For each toast it should set something new from the database.

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares