Browse By

Customizing Toast In Android

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a “Draft saved” toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

The default implementation of a  Toast shows a simple text at the bottom of the screen. Code snippets to create a default Toast message.

//display in short period of time
Toast.makeText(getApplicationContext(), "Pulse 7 Rocks", Toast.LENGTH_SHORT).show();

//display in long period of time
Toast.makeText(getApplicationContext(), "Pulse 7 Rocks", Toast.LENGTH_LONG).show();

We can change this  default implementation of Toast. We can change the size , color, style and other properties of the text being displayed in Toast.   Not only this we can also add image and other views in Toast.

In this tutorial we will create simple android application which will display Custom Toast message in center of screen. We will create custom layout and set in toast to make it more beautiful.

Direct Download Full Source Code



Customizing Toast In Android

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Open your main.xml and design a simple layout for Button. On click of button we will display Custom Toast.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<TextView android:textColor="#fff" android:textSize="20dp"
    		android:layout_height="wrap_content" android:id="@+id/textView1"
    		android:text="Pulse 7 Demo" android:layout_width="wrap_content"
    	<TextView android:textColor="#fff" android:textSize="15dp"
    		android:layout_height="wrap_content" android:id="@+id/textView2"
    		android:text="Customizing Toast In Android " android:layout_width="wrap_content"
    		android:layout_gravity="center_horizontal" android:layout_weight="1"></TextView>
    	<Button android:layout_width="match_parent" android:id="@+id/bntSuccess"
    		android:text="Success Notification at Middle of Screen"
    		android:layout_gravity="bottom" android:layout_height="wrap_content"></Button>
  3. Create new layout xml file in res/layout/ layout folder, for customizing Toast.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:layout_width="match_parent" android:id="@+id/custom_toast_layout_id"
    	android:orientation="horizontal" android:padding="10dp"
    	android:layout_height="wrap_content" android:background="#d4ffcd">
    	<ImageView android:layout_height="wrap_content"
    		android:layout_width="wrap_content" android:id="@+id/imageView1"
    		android:src="@drawable/success" android:layout_gravity="center_vertical"></ImageView>
    	<LinearLayout android:layout_width="wrap_content"
    		android:id="@+id/linearLayout1" android:orientation="vertical"
    		android:layout_height="wrap_content" android:paddingLeft="15dp">
    		<TextView android:layout_height="wrap_content" android:id="@+id/textView1"
    			android:layout_width="wrap_content" android:textColor="#54a846"
    			android:textSize="20dp" android:text="Record Saved Successfully"></TextView>
    		<TextView android:id="@+id/textView2" android:layout_width="wrap_content"
    			android:layout_height="wrap_content" android:textColor="#404040"
    			android:textSize="15dp" android:text="Pulse 7 Demo for custom Toast"></TextView>
  4. Now inflate custom layout in Toast using LayoutInflater.
    package com.vrs.pulse7toast;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    public class Pulse7ToastActivity extends Activity implements OnClickListener {
    	// Widget GUI
    	Button btnSuccess;
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    		// Button Widget Init
    		btnSuccess = (Button) findViewById(;
    	public void onClick(View v) {
    		if (v == btnSuccess) {
    			// Inflate the Layout
    			LayoutInflater inflater = getLayoutInflater();
    			View layout = inflater.inflate(R.layout.custom_toast,
    					(ViewGroup) findViewById(;
    			// Create Custom Toast
    			Toast toast = new Toast(getApplicationContext());
    			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  5. Now Execute Application

    Customizing Toast in Android

    Custom Toast at Middle of Screen

Points to Remember

  • getLayoutInflater() is used to get LayoutInflater class.
  • inflate() of LayoutInflater class inflate your custom toast view layout.
  • Using setView() of Toast class you can set custom layout.

I hope you like this article. Share your views to improve content.

Download Full Source Code




  • Frans Appel

    I use it frequently and I have it noted in my Infoblocks.

    How can I get a transparent background ?

    • Frans Appel

      I have found it: