Android development step by step 32: Toast
Toast has the English name Tusi. In Android, this class is used to pop up the prompt information. I think the sdk author thinks that the prompt message is like a piece of Toast. We will not talk about this theory much, so we will start to practice it.
Step 1: Design the page
Home Page toastview. xml
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Custom toast page mmtoast. xml
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_height = "wrap_content" android: layout_width = "wrap_content"
Android: background = "# ffffffff" android: orientation = "vertical"
Android: id = "@ + id/llToast">
Android: layout_margin = "1dip" android: textColor = "# ffffffff"
Android: layout_width = "fill_parent" android: gravity = "center"
Android: background = "# bb000000" android: id = "@ + id/tvTitleToast"/>
Android: orientation = "vertical" android: id = "@ + id/llToastContent"
Android: layout_marginLeft = "1dip" android: layout_marginRight = "1dip"
Android: layout_marginBottom = "1dip" android: layout_width = "wrap_content"
Android: padding = "15dip" android: background = "#44000000">
Android: layout_gravity = "center" android: layout_width = "wrap_content"
Android: id = "@ + id/imgToast"/>
Android: paddingRight = "10dip" android: paddingLeft = "10dip"
Android: layout_width = "wrap_content" android: gravity = "center"
Android: textColor = "# ff000000" android: id = "@ + id/tvTextToast"/>
Step 2: Design actti1_toastactivity. java
/**
*
*/
Package com. figo. helloworld;
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. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Import android. widget. Toast;
/**
* @ Author Administrator
*
*/
Public class ToastActivity extends Activity implementsOnClickListener {
Handler handler = new Handler (); // helps the main thread and subthread communicate
@ Override
Public void onCreate (BundlesavedInstanceState ){
Super. onCreate (savedInstanceState );
// Find the page
SetContentView (R. layout. toastview );
// Add button event
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: // simple toast
Toast. makeText (getApplicationContext (), "Default Toast style ",
Toast. LENGTH_SHORT). show ();
Break;
CaseR. id. btnSimpleToastWithCustomPosition: // toast of the custom position
Toast = Toast. makeText (getApplicationContext (), "Custom location Toast ",
Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Break;
CaseR. id. btnSimpleToastWithImage: // toast with image
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: // custom toast
LayoutInflaterinflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout
. FindViewById (R. id. imgToast );
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 = newToast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 100, 60 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();
Break;
CaseR. id. btnRunToastFromOtherThread: // after other threads are executed, toast is displayed in the main thread.
New Thread (new Runnable (){
Public void run (){
ShowToast ();
}
}). Start ();
Break;
}
}
}
Step 3: Register Activity with AndroidManifest. xml
Step 4. Running Effect