Main Tutorials

Android Toast example

In Android, Toast is a notification message that pop up, display a certain amount of time, and automtaically fades in and out, most people just use it for debugging purpose.

Code snippets to create a Toast message :


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

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

In this tutorial, we will show you two Toast examples :

  1. Normal Toast view.
  2. Custom Toast view.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Normal Toast View

Simple Toast example.

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        
    <Button
        android:id="@+id/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast" />
    
</LinearLayout>

File : MainActivity.java


package com.mkyong.android;

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

public class MainActivity extends Activity {

	private Button button;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonToast);
		
		button.setOnClickListener(new OnClickListener() {
			 
			  @Override
			  public void onClick(View arg0) {
				  
			     Toast.makeText(getApplicationContext(), 
                               "Button is clicked", Toast.LENGTH_LONG).show();

			  }
		});
	}
}

See demo, when a button is clicked, display a normal Toast message.

android toast example

2. Custom Toast View

Enchance above example by customize the original Toast view.

File : res/layout/custom_toast.xml – This is the custom Toast view.


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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />

</LinearLayout>

File : MainActivity.java – Read comment, to get above custom view and attach to Toast.


package com.mkyong.android;

import android.app.Activity;
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;

public class MainActivity extends Activity {

	private Button button;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonToast);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				// get your custom_toast.xml ayout
				LayoutInflater inflater = getLayoutInflater();

				View layout = inflater.inflate(R.layout.custom_toast,
				  (ViewGroup) findViewById(R.id.custom_toast_layout_id));

				// set a dummy image
				ImageView image = (ImageView) layout.findViewById(R.id.image);
				image.setImageResource(R.drawable.ic_launcher);

				// set a message
				TextView text = (TextView) layout.findViewById(R.id.text);
				text.setText("Button is clicked!");

				// Toast...
				Toast toast = new Toast(getApplicationContext());
				toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
				toast.setDuration(Toast.LENGTH_LONG);
				toast.setView(layout);
				toast.show();
			}
		});
	}
}

See demo, when a button is clicked, display the custom Toast message.

Android Toast example

Download Source Code

Download it – Android-Toast-Example.zip (16 KB)

References

  1. Android Toast example
  2. Android Toast JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
HUSSAIN PATANWALA
11 years ago

Hi Yong,

I see your examples very structured and easy to understand.

In (https://mkyong.com/android/android-toast-example/) you explained it very well. However I was looking for Toast Message which appears to tell app user what to do.

Example: User Launches App –> There is an image button –> toast message to tell user “touch / click Image to move to next page”.

If possible can you please update and let me know about it.

I just started building apps with no knowledge of programing.

Regards,
HUSSAIN

Durval Pires
11 years ago

Hi! I think what you are looking for is a Dialog! 🙂

anonymous
4 years ago

:D:D

saroj khatiwoda
8 years ago

Thank you, Good Example,

guache
9 years ago

Thanks so much dude! You are a rockstar!!!…

nilesh kumar
9 years ago
Gautam
9 years ago

Hello,
This program just works fine.
But I want to convert it into a library so that I do not have to waste my time in coding for my future projects.
How can this be done??

Mohd Aquib
9 years ago

Hello,
I want to make a custom toast on which a message will print as well as a button so that on clicking that on clicking on that button my application goes to another activity…
Thanks

pradeep
10 years ago

how to write a junit test cases for the displaying of the toast timings in the android apps.?
My requirement is how to verify the toast message timings is displaying ?
Can you please help me. Your help will be greatly appreciated.

Michael
10 years ago

Hello,

I get for all examples the error messages:
“Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.”
“MyAndroidApp] Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.”

What can I do to avoid the errors?

Michael

Law Long
10 years ago

Thx for your example!

Mayaa
10 years ago

Nice example mkyong..Even this http://www.compiletimeerror.com/2013/07/android-toast-in-android-example.html might help.. Have a look..

James Green
10 years ago

sorry guys forgot to paste the link in previous message for Android Toast Tutorial that I found on Youtube. Link: http://www.youtube.com/watch?v=sVx46awjtFQ&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa&index=27

James Green
10 years ago

Hi Mkyong,
Thanks for making this tutorial on Toast in Android. But I also came across one more video which demonstrates how to display Toast on screen in Android as well. Though in addition to your tutorial this video can also help other who visits your blog.

Thanks for reading the message, keep on making the great stuff 🙂

Regards,

josh
10 years ago

nice example. also i got other toast example in

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

davide
11 years ago

Good tutorial..but the manifest?

Jagdish
11 years ago

Hi MK YONG,
your tips and tutorials are always helpful to me.
Can you please mw with below issues :
1) How to call a methods decalared in activity from another activity or is there any way to dcelare that method in java class (non-activity) and call it from activity ?

Many Thanks in advance !!!

Jagdish.

Chaim Amram
11 years ago

Hi, I am professionalizing in Android these days and would like to thank you very much for clear and useful contributions
Regards,
Chaim

Varshu
11 years ago

Your site layout is very good. I liked it.