Toast style and custom Toast in Android

Source: Internet
Author: User

Toast style and custom Toast in Android

Android: id = "@ + id/toast_layout"
Android: layout_width = "200dip"
Android: layout_height = "fill_parent"
Android: background = "# f0f0f0"
Android: orientation = "vertical">

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


Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "#999999"
Android: orientation = "horizontal">

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


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



 

-------------------------- 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. style+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 Toast display position style", Toast. LENGTH_LONG );
/** Set the display position to center */
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Break;

Case R. id. style3_btn:
Toast = Toast. makeText (ToastActivity. this, "Custom Toast style with image", Toast. LENGTH_LONG );
LayoutInflater inflater = getLayoutInflater ();
/** Custom Layout */
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 ("the Toast displayed for a long time is a custom Dialog! ");
Builder = new AlertDialog. Builder (this );
Builder. setView (styleView );
Dialog = builder. create ();
Dialog. show ();
Break;

Default:
Break;
}
}
}

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.