Introduction
Toast is a pop-up Message that allows you to easily notify users of some time, such as saving data to the SD card. It is worth noting that you cannot cancel Toast. In most cases, Toast is just a short message, but you can also customize the Toast interface.
Create Standard Toast
The standard Toast can be created using the static Toast method makeText:
Toast. makeText (getApplicationContext (), "Hello, The Code Project! ",
Toast. LENGTH_SHORT). show ();
Toast. makeText (getApplicationContext (), "Hello, The Code Project! ",
Toast. LENGTH_SHORT). show (); parameters are the application context, the displayed message content, and the displayed delay. You can also use R to call the content of the resource file, such as R. string. hello_codeproject. The delay displayed by Message can be either LENGTH_SHORT or LENGTH_LONG, Which is LENGTH_SHORT by default. You can also set the Latency by calling the setDuration method.
Set Toast location
You can set the Toast position on the screen by calling the following method:
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show (); the first parameter sets the location, and the second parameter defines the offset pixel relative to the location of the first parameter.
Add images to standard Toast
You need to create an ImageView object, call the setImageResource method, and add an image to Toast.
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
LinearLayout toastView = (LinearLayout) toast. getView ();
ImageView imageCodeProject = new ImageView (getApplicationContext ());
ImageCodeProject. setImageResource (R. drawable. codeprojectlogo );
ToastView. addView (imageCodeProject, 0 );
Toast. show ();
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
LinearLayout toastView = (LinearLayout) toast. getView ();
ImageView imageCodeProject = new ImageView (getApplicationContext ());
ImageCodeProject. setImageResource (R. drawable. codeprojectlogo );
ToastView. addView (imageCodeProject, 0 );
Toast. show ();
Effect
Create Toast for custom Layout
First, create the layout of Toast for custom layout:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
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">
<TextView
Android: layout_height = "wrap_content"
Android: layout_margin = "1dip"
Android: textColor = "# ffffffff"
Android: layout_width = "fill_parent"
Android: gravity = "center"
Android: background = "# bb000000"
Android: id = "@ + id/tvTitleToast"/>
<LinearLayout
Android: layout_height = "wrap_content"
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">
<ImageView
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_width = "wrap_content"
Android: id = "@ + id/tvImageToast"/>
<TextView
Android: layout_height = "wrap_content"
Android: paddingRight = "10dip"
Android: paddingLeft = "10dip"
Android: layout_width = "wrap_content"
Android: gravity = "center"
Android: textColor = "# ff000000"
Android: id = "@ + id/tvTextToast"/>
</LinearLayout>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
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">
<TextView
Android: layout_height = "wrap_content"
Android: layout_margin = "1dip"
Android: textColor = "# ffffffff"
Android: layout_width = "fill_parent"
Android: gravity = "center"
Android: background = "# bb000000"
Android: id = "@ + id/tvTitleToast"/>
<LinearLayout
Android: layout_height = "wrap_content"
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">
<ImageView
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_width = "wrap_content"
Android: id = "@ + id/tvImageToast"/>
<TextView
Android: layout_height = "wrap_content"
Android: paddingRight = "10dip"
Android: paddingLeft = "10dip"
Android: layout_width = "wrap_content"
Android: gravity = "center"
Android: textColor = "# ff000000"
Android: id = "@ + id/tvTextToast"/>
</LinearLayout>
</LinearLayout>
The Toast we created has a header, an image, and a message. Now we need to apply layout to the Toast we created:
LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast );
Image. setImageResource (R. drawable. codeprojectlogo );
TextView title = (TextView) layout. findViewById (R. id. tvTitleToast );
Title. setText ("Attention ");
TextView text = (TextView) layout. findViewById (R. id. tvTextToast );
Text. setText ("Hello, The Code Project! ");
Toast toast = new Toast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();
LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast );
Image. setImageResource (R. drawable. codeprojectlogo );
TextView title = (TextView) layout. findViewById (R. id. tvTitleToast );
Title. setText ("Attention ");
TextView text = (TextView) layout. findViewById (R. id. tvTextToast );
Text. setText ("Hello, The Code Project! ");
Toast toast = new Toast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();
From xinem's column