AndroidUI control series: Toast (prompt)

Source: Internet
Author: User
Toast is used to display some help prompts to users. The following is an example of how Toast works. it defines your own Toast. Toast is used to display some help/prompts to users. The following is an example of how Toast works. it defines your own Toast.

Note:

LENGTH_LONG-Display views or text prompts for a long time

LENGTH_SHORT-Display views or text prompts in a short time

SetGravity (int gravity, int xOffset, int yOffset)-sets the position where the prompt should be displayed on the screen.

SetDuration (int duartion)-set the display duration
1. default effect

Code

Toast. makeText (getApplicationContext (), "default Toast style", Toast. LENGTH_SHORT). show ();

2. Custom display position effect

Code

Toast = Toast. makeText (getApplicationContext (), "custom location Toast", Toast. LENGTH_LONG); toast. setGravity (Gravity. CENTER, 0, 0); toast. show ();

3. image effects

Toast = Toast. makeText (getApplicationContext (), "Toast with image", Toast. LENGTH_LONG); toast. setGravity (Gravity. CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast. getView (); ImageView imageCodeProject = new ImageView (getApplicationContext (); imageCodeProject. setImageResource (R. drawable. icon); toastView. addView (imageCodeProject, 0); toast. show ();

4. fully customized results

Code

LayoutInflater inflater = getLayoutInflater (); View layout = inflater. inflate (R. layout. custom, (ViewGroup) findViewById (R. id. llToast); ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast); image. setImageResource (R. drawable. icon); TextView title = (TextView) layout. findViewById (R. id. tvTitleToast); title. setText ("Attention"); TextView text = (TextView) layout. findViewById (R. id. tvTextToast); text. setText ("fully custom Toast"); toast = new Toast (getApplicationContext (); toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40); toast. setDuration (Toast. LENGTH_LONG); toast. setView (layout); toast. show ();

5. other threads

new Thread(new Runnable() {      public void run() {       showToast();      }     }).start();

Complete Code

1. Main, java

Package com. wjq. toast;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. View. OnClickListener;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Import android. widget. Toast;
Public class Main extends Activity implements OnClickListener {
Handler handler = new Handler ();
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
FindViewById (R. id. btnSimpleToast). setOnClickListener (this );
FindViewById (R. id. btnSimpleToastWithCustomPosition). setOnClickListener (
This );
FindViewById (R. id. btnSimpleToastWithImage). setOnClickListener (this );
FindViewById (R. id. btnCustomToast). setOnClickListener (this );
FindViewById (R. id. btnRunToastFromOtherThread). setOnClickListener (this );
}
Public void showToast (){
Handler. post (new Runnable (){
@ Override
Public void run (){
Toast. makeText (getApplicationContext (), "I'm from another thread! ",
Toast. LENGTH_SHORT). show ();
}
});
}
@ Override
Public void onClick (View v ){
Toast toast = null;
Switch (v. getId ()){
Case R. id. btnSimpleToast:
Toast. makeText (getApplicationContext (), "default Toast style ",
Toast. LENGTH_SHORT). show ();
Break;
Case R. id. btnSimpleToastWithCustomPosition:
Toast = Toast. makeText (getApplicationContext (),
"Custom location Toast", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Break;
Case R. id. btnSimpleToastWithImage:
Toast = Toast. makeText (getApplicationContext (), "Toast with image", Toast. LENGTH_LONG); toast. setGravity (Gravity. CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast. getView (); ImageView imageCodeProject = new ImageView (getApplicationContext (); imageCodeProject. setImageResource (R. drawable. icon); toastView. addView (imageCodeProject, 0); toast. show ();
Break;
Case R. id. btnCustomToast:
LayoutInflater inflater = getLayoutInflater (); View layout = inflater. inflate (R. layout. custom, (ViewGroup) findViewById (R. id. llToast); ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast); image. setImageResource (R. drawable. icon); TextView title = (TextView) layout. findViewById (R. id. tvTitleToast); title. setText ("Attention"); TextView text = (TextView) layout. findViewById (R. id. tvTextToast); text. setText ("fully custom Toast"); toast = new Toast (getApplicationContext (); toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40); toast. setDuration (Toast. LENGTH_LONG); toast. setView (layout); toast. show ();
Break;
Case R. id. btnRunToastFromOtherThread:
New Thread (new Runnable () {public void run () {showToast () ;}}). start ();
Break;
}
}
}

2. main, xml

   
   
    
    
    
    
    
 

3. custom. xml

   
   
    
      
       
     
    
 

The above is the content of the Android UI control series: Toast (prompt). For more information, see PHP Chinese network (www.php1.cn )!

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.