Android Toast Encyclopedia (five scenarios) build your own toast

Source: Internet
Author: User

Toast is used to display some help/hints to the user. Below I've done 5 effects to illustrate the power of toast and define a toast that belongs to you.

1. Default effect

Code

Toast.maketext (Getapplicationcontext (), default toast style,
Toast.length_short). Show ();

2. Customize the display position effect

Code

Toast = Toast.maketext (Getapplicationcontext (),
"Custom Location Toast", Toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();

3. With picture effect

Code

Toast = Toast.maketext (Getapplicationcontext (),
"Toast with pictures", 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 customizable effects

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 customizable 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 ();

Full 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 pictures", 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 customizable 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

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" android:padding= "5dip" android:gravity= "Center" >
<button android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent" android:id= "@+id/btnsimpletoast"
android:text= "Default" ></Button>
<button android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent" android:text= "Custom display location"
Android:id= "@+id/btnsimpletoastwithcustomposition" ></Button>
<button android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent" android:id= "@+id/btnsimpletoastwithimage"
android:text= "with pictures" ></Button>
<button android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent" android:text= "fully customizable"
Android:id= "@+id/btncustomtoast" ></Button>
<button android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent" android:text= "Other Threads"
Android:id= "@+id/btnruntoastfromotherthread" ></Button>

</LinearLayout>

3.custom.xml

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

Android Toast Encyclopedia (five scenarios) build your own toast

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.