Android five different styles of toast

Source: Internet
Author: User

Nonsense not much to say, directly on the code:


Package com.otn.android.toast;


Import android.app.Activity;

Import Android.app.AlertDialog;

Import Android.os.Bundle;

Import android.view.Gravity;

Import Android.view.LayoutInflater;

Import Android.view.View;

Import Android.view.ViewGroup;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.ImageView;

Import Android.widget.LinearLayout;

Import Android.widget.TextView;

Import Android.widget.Toast;


public class Mytoast extends Activity implements Onclicklistener {

private static final String toastbtn_1 = "This is the default toast display";

private static final String toastbtn_2 = "This is a toast display for a custom location";

private static final String Toastbtn_3 = "This is a toast display with pictures";

private static final String Toastbtn_4 = "This is a fully customizable toast display";

private static final String Toastbtn_5 = "This is a long time toast display";

Private Button toastbtn_1, Toastbtn_2, Toastbtn_3, Toastbtn_4, toastbtn_5;

Private toast toast = null;


@Override

protected void OnCreate (Bundle savedinstancestate) {

TODO auto-generated Method Stub

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

toastbtn_1 = (Button) Findviewbyid (r.id.button_1);

Toastbtn_2 = (Button) Findviewbyid (r.id.button_2);

Toastbtn_3 = (Button) Findviewbyid (r.id.button_3);

Toastbtn_4 = (Button) Findviewbyid (r.id.button_4);

Toastbtn_5 = (Button) Findviewbyid (r.id.button_5);

Toastbtn_1.setonclicklistener (this);

Toastbtn_2.setonclicklistener (this);

Toastbtn_3.setonclicklistener (this);

Toastbtn_4.setonclicklistener (this);

Toastbtn_5.setonclicklistener (this);

}


@Override

public void OnClick (View v) {

TODO auto-generated Method Stub

Alertdialog.builder Builder;

Alertdialog Dialog;

Switch (V.getid ()) {

Case R.ID.BUTTON_1:

Toast.maketext (This, toastbtn_1, Toast.length_long). Show ();

Break


Case R.id.button_2:

Toast = Toast.maketext (Getapplicationcontext (), Toastbtn_2,

Toast.length_long);

Toast.setgravity (gravity.center, 0, 0);

Toast.show ();

Break


Case R.id.button_3:

Toast = Toast.maketext (Getapplicationcontext (), Toastbtn_3,

Toast.length_long);

Toast.setgravity (Gravity.center, 50,-100);

LinearLayout layout = (linearlayout) Toast.getview ();

ImageView image = New ImageView (Getapplicationcontext ());

Image.setimageresource (R.drawable.wallpaper_tree_small);

Layout.addview (image, 0);

Toast.show ();

Break


Case R.id.button_4:

Layoutinflater inflater = Getlayoutinflater ();

View view = Inflater.inflate (R.layout.userdefinedtoast,

(ViewGroup) Findviewbyid (r.id.toast_layout));

TextView Txtview_title = (TextView) view

. Findviewbyid (R.id.txt_title);

TextView Txtview_context = (TextView) view

. Findviewbyid (R.id.txt_context);

ImageView ImageView = (ImageView) view

. Findviewbyid (R.id.image_toast);

Toast = new Toast (Getapplicationcontext ());

Toast.setgravity (gravity.center, 0, 0);

Toast.setduration (Toast.length_long);

Toast.setview (view);

Toast.show ();

Break

Case R.id.button_5:

Layoutinflater inflater1 = Getlayoutinflater ();

View view1 = inflater1.inflate (R.layout.userdefinedtoast,

(ViewGroup) Findviewbyid (r.id.toast_layout));

TextView txtview_title1 = (TextView) view1

. Findviewbyid (R.id.txt_title);

TextView txtview_context1 = (TextView) view1

. Findviewbyid (R.id.txt_context);

ImageView imageView1 = (ImageView) view1

. Findviewbyid (R.id.image_toast);

Builder = new Alertdialog.builder (this);

Builder.setview (View1);

Dialog = Builder.create ();

Dialog.show ();

Break


Default

Break

}

}


}


Android five different styles of toast

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.