Custom Toasts in ANDROID.

By | October 5, 2010

First, just show an image inside a toast:

package com.Ch.Example.pack;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;


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

        showToast();
    }
	private void showToast()
	{
		Toast toast = new Toast(getApplicationContext());
		ImageView view = new ImageView(getApplicationContext());
		view.setImageResource(R.drawable.fish1);
		toast.setView(view);
		toast.show();
	}
}

This method just displays a text toast:

Context context = getApplicationContext();
CharSequence text = "hai i am here";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 50, 50);
toast.show();


And this method displays a Toast that contains both an image and text:

CharSequence text = "hai i am here";
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
View textView = toast.getView();
LinearLayout lay = new LinearLayout(getApplicationContext());
lay.setOrientation(LinearLayout.VERTICAL);
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.fish1);
lay.addView(view);
lay.addView(textView);
toast.setView(lay);
toast.show();

Leave a Reply

Your email address will not be published. Required fields are marked *