Toast styles and custom toasts in Android

Source: Internet
Author: User

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/toast_layout"
Android:layout_width= "200dip"
android:layout_height= "Fill_parent"
Android:background= "#f0f0f0"
android:orientation= "Vertical" >

<textview
Android:id= "@+id/txt_title"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Center|top"
android:text= "@string/toast_text_1"
Android:textcolor= "#ffffff"
Android:textsize= "20dip" >
</TextView>

<linearlayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:background= "#999999"
android:orientation= "Horizontal" >

<imageview
Android:id= "@+id/image_toast"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginright= "10dip"
android:src= "@drawable/icon_message_nub" >
</ImageView>

<textview
Android:id= "@+id/txt_context"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Center|right"
android:text= "@string/toast_text_2"
Android:textcolor= "#ffffff"
Android:textsize= "15dip" >
</TextView>
</LinearLayout>

</LinearLayout>

--------------------------Activity Code------------------------

public class Toastactivity extends Activity implements Onclicklistener {
Private Button style1btn;
Private Button style2btn;
Private Button style3btn;
Private Button style4btn;
Private toast toast = null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.test_main);
Initviews ();
Initlisteners ();
}

private void Initviews () {
STYLE1BTN = (Button) Findviewbyid (R.ID.STYLE1_BTN);
STYLE2BTN = (Button) Findviewbyid (R.ID.STYLE2_BTN);
STYLE3BTN = (Button) Findviewbyid (R.ID.STYLE3_BTN);
STYLE4BTN = (Button) Findviewbyid (R.ID.STYLE4_BTN);
}

private void Initlisteners () {
Style1btn.setonclicklistener (this);
Style2btn.setonclicklistener (this);
Style3btn.setonclicklistener (this);
Style4btn.setonclicklistener (this);
}

@Override
public void OnClick (View v) {
Alertdialog.builder Builder;
Alertdialog Dialog;
Switch (V.getid ()) {
Case R.ID.STYLE1_BTN:
Toast.maketext (toastactivity.this, "system default Toast style", Toast.length_long). Show ();
Break

Case R.ID.STYLE2_BTN:
Toast = Toast.maketext (toastactivity.this, "change the style of Toast display position", toast.length_long);
/** setting display position is centered */
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();
Break

Case R.ID.STYLE3_BTN:
Toast = Toast.maketext (Toastactivity.this, "Custom toast style with pictures", toast.length_long);
Layoutinflater inflater = Getlayoutinflater ();
/** Custom Layouts */
View view = Inflater.inflate (R.layout.my_custom_toast, (ViewGroup) Findviewbyid (r.id.toast_layout));
Toast = new Toast (Getapplicationcontext ());
Toast.setgravity (gravity.center, 0, 0);
Toast.setduration (Toast.length_long);
Toast.setview (view);
Toast.show ();
Break

Case R.ID.STYLE4_BTN:
Layoutinflater inflater1 = Getlayoutinflater ();
View StyleView = Inflater1.inflate (R.layout.my_custom_toast, (ViewGroup) Findviewbyid (r.id.toast_layout));
TextView TV = (TextView) Styleview.findviewbyid (R.id.txt_context);
Tv.settext ("Long-displayed toast is a dialog! of your own definition");
Builder = new Alertdialog.builder (this);
Builder.setview (StyleView);
Dialog = Builder.create ();
Dialog.show ();
Break

Default
Break
}
}
}

Toast styles and custom toasts in Android

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.