Home > Tutorials > Playing with Toast message notification

Playing with Toast message notification


Toast is very useful when you need to notify something to users and don’t require interactive responses.

One of the very basic toast like this:

Toast 01

Toast 01

public static void displayToast(Context c, String text, int duration) {
    Toast.makeText(c, text, duration).show();
}

By default, toast will stay close to the bottom of the screen.
If you want to set it in other location, just use setGravity() to define the location to display.

Toast 02

Toast 02

	public static void displayToast(Context c, String text, int duration, int gravity) {
		Toast t = Toast.makeText(c, text, duration);
		t.setGravity(gravity, 0, 0);
		t.show();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			QuickToast.displayToast(this, "A Toast in the center of the screen", Toast.LENGTH_LONG, Gravity.CENTER);
		}
		return super.onTouchEvent(event);
	}

It’s quite simple! However, in case you want to create a custom layout for toast, like an image, some texts…is it possible? The answer is Yes, and Yes!

This is a sample of a custom one:

Custom Toast

Custom Toast

	public static void displayToastFromResource(Context c, int duration, int gravity, ViewGroup parent) {
		// get layout inflater
		LayoutInflater inflater = LayoutInflater.from(c);
		// get the desired layout for toast
		View view = inflater.inflate(R.layout.layout_toast, parent);
		// get image view control
		ImageView ivIcon = (ImageView)view.findViewById(R.id.ivIcon);
		// set icon to image view
		ivIcon.setImageResource(R.drawable.icon);
		// get title text view
		TextView tvTitle = (TextView)view.findViewById(R.id.tvTitle);
		// set title content
		tvTitle.setText("I Love You");
		// get description text view
		TextView tvDesc = (TextView)view.findViewById(R.id.tvDesc);
		// set description content
		tvDesc.setText("It's been years since I met you !");

		// create toast from context
		Toast t = new Toast(c);
		// set center
		t.setGravity(Gravity.CENTER, 0, 0);
		// set display duration
		t.setDuration(duration);
		// set view layout to toast
		t.setView(view);
		// display toast message
		t.show();
	}

Oh well, you may wanna ask how I implemented those toast. It’s because I created a wrapper class QuickToast for handling toast settings and custom toasts.
This is the full source code I use for this tutorial:

1. QuickToast.java

package pete.android.study;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class QuickToast {
	public static void displayToast(Context c, String text, int duration) {
		Toast.makeText(c, text, duration).show();
	}

	public static void displayToast(Context c, int id, int duration) {
		Toast.makeText(c, id, duration).show();
	}

	public static void displayToast(Context c, String text, int duration, int gravity) {
		Toast t = Toast.makeText(c, text, duration);
		t.setGravity(gravity, 0, 0);
		t.show();
	}

	public static void displayToast(Context c, String text, int duration, int gravity, int[] offset) {
		Toast t = Toast.makeText(c, text, duration);
		t.setGravity(gravity, offset[0], offset[1]);
		t.show();
	}

	public static void displayToastFromResource(Context c, int duration, int gravity, ViewGroup parent) {
		// get layout inflater
		LayoutInflater inflater = LayoutInflater.from(c);
		// get the desired layout for toast
		View view = inflater.inflate(R.layout.layout_toast, parent);
		// get image view control
		ImageView ivIcon = (ImageView)view.findViewById(R.id.ivIcon);
		// set icon to image view
		ivIcon.setImageResource(R.drawable.icon);
		// get title text view
		TextView tvTitle = (TextView)view.findViewById(R.id.tvTitle);
		// set title content
		tvTitle.setText("I Love You");
		// get description text view
		TextView tvDesc = (TextView)view.findViewById(R.id.tvDesc);
		// set description content
		tvDesc.setText("It's been years since I met you !");

		// create toast from context
		Toast t = new Toast(c);
		// set center
		t.setGravity(Gravity.CENTER, 0, 0);
		// set display duration
		t.setDuration(duration);
		// set view layout to toast
		t.setView(view);
		// display toast message
		t.show();
	}
}

2. MainActivity.java

package pete.android.study;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			QuickToast.displayToastFromResource(this, Toast.LENGTH_LONG, Gravity.CENTER, (ViewGroup)findViewById(R.id.llRoot));
		}
		return super.onTouchEvent(event);
	}
}

You can download full source code from one of these servers: Mediafire | Megaupload | Rapidshare | FreeFileHosting

 

Hope you enjoy it!

 

Cheers,

Pete Houston

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: