Android Development Series (23): provides a Toast prompt box with image prompts.

Source: Internet
Author: User

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:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.