Android Development Series (23): provides a Toast prompt box with image prompts.
In Android, Toast is a common message prompt box, but the default message prompt box is a line of plain text, so we can set some other message prompts for it, such as with a piece.
Implementation is simple:
Define a Layout View and set Toast to display the custom View.
Here, we set a LinearLayout container, and then add images and text information to the container. Then, set the container to the Toast object for display.
First create an Android project, and then edit the main. xml file:
Two buttons are defined here. One is the default Toast message prompt, and the other is the message prompt with an image.
Next, we can edit the java code on the main interface: ToastTest. java
Import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; import android. widget. toast; public class ToastTest extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button simple = (Button) findViewById (R. id. simple); // bind the event listener simple to the Click Event of the button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View source) {// create a Toast prompt message Toast toast = Toast. makeText (ToastTest. this, "simple prompt information" // sets the Toast message duration, Toast. LENGTH_SHORT); toast. show () ;}}); Button bn = (Button) findViewById (R. id. bn); // bind event listener bn to the Click Event of the button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View source) {// create a Toast prompt message Toast toast = new Toast (ToastTest. this); // set the Toast display position toast. setGravity (Gravity. CENTER, 0, 0); // create an ImageViewImageView image = new ImageView (ToastTest. this); image. setImageResource (R. drawable. tools); // create a LinearLayout container LinearLayout ll = new LinearLayout (ToastTest. this); // Add images and original Viewll to LinearLayout. addView (image); // create an ImageViewTextView textView = new TextView (ToastTest. this); textView. setText ("prompt message with image"); // set the font size and color textView in the text box. setTextSize (30); textView. setTextColor (Color. MAGENTA); ll. addView (textView); // set Toast to display custom Viewtoast. setView (ll); // set the Toast display time toast. setDuration (Toast. LENGTH_LONG); toast. show ();}});}}
We can get the following: