Feb 18, 2011

Toast with image

It's a sample code to display Toast with image.

Toast with image

package com.AndroidImageToast;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

Toast ImageToast = new Toast(getBaseContext());
LinearLayout toastLayout = new LinearLayout(getBaseContext());
toastLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView image = new ImageView(getBaseContext());
TextView text = new TextView(getBaseContext());
image.setImageResource(R.drawable.icon);
text.setText("Hello!");
toastLayout.addView(image);
toastLayout.addView(text);
ImageToast.setView(toastLayout);
ImageToast.setDuration(Toast.LENGTH_LONG);
ImageToast.show();
}
}


1 comment:

  1. Thanks! :-D

    I use a light theme, how can I have sort of the standard dark background behind the toast? With this the image just floats there.

    Alternatively if I could detect if it's dark or light behind it I can use a dark or light version of icon and text as appropriate. If the keyboard is on on some layouts it would be dark background.

    But if I could just have the regular toast background that would be best if you can let me know how?

    ReplyDelete

Infolinks In Text Ads