Effects of Android Toast

Source: Internet
Author: User

From: http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html

Toast is used to display some help/prompts to users. The following is an example of how Toast works. It defines your own Toast.

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

 

Code

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 customized 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

Code

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 customized 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;

}

}
}

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.