Let the hint be more personal--Define your own toast

Source: Internet
Author: User

Just when looking at the Android API, I accidentally saw the use of toast. Think about the projects you've done before, suggesting that words always seem so stiff. Mastering a custom toast is quite simple, don't neglect this development technique, make a little of it, and let yourself become excellent.

Usually we use toasts to instantiate a Toast object with the Maketext () method. The method requires three parameters: the current context of the app, the text message, and the duration of the toast. The method returns an instantiated toast object that can be displayed with the show () method

Context context = Getapplicationcontext (); Charsequence Text = "Hello toast!" Intduration = Toast.length_short; Toast Toast = Toast.maketext (context, text, duration). Show ();
Most of the cases we use in the activity (take mainactivity as an example)

Toast.maketext (mainactivity.this, "cue words", "n"). Show ();


If you want toast to be more user-friendly, then customization is the only way.

First, we design an interface for the toast we want (as we design the view, we need an XML layout file), the layout ID is toast_layout_root, and the name is Mytoast

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/toast_layout_ Root "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:background=" # DAAA "    android:orientation=" horizontal "    android:padding=" 10DP ">    <imageview android:id=        " @+ Id/image "        android:layout_width=" wrap_content "        android:layout_height=" fill_parent "        android:layout_ marginright= "10DP"/>    <textview        android:id= "@+id/text"        android:layout_width= "Wrap_content"        android:layout_height= "fill_parent"        android:gravity= "center"        android:textcolor= "#ffffff"/> </LinearLayout>
then we can run our toast in our code (specifically I use mainactivity for example)

Package Com.example.mytoast;import Android.app.activity;import Android.os.bundle;import android.view.Gravity; Import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity {private Button click; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); click = (Button) Findviewbyid (R.id.click); Click.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub <span style= "color: #ff0000;" >layoutinflater inflater = Getlayoutinflater (); View layout = Inflater.inflate (R.layout.mytoast, (ViewGroup) Findviewbyid (r.id.toast_layout_root)); ImageView im Age = (ImageView) Layout.findviewbyid (r.id.image);Mage.setimageresource (R.drawable.ic_launcher); TextView Text = (TextView) Layout.findviewbyid (R.id.text); Text.settext ("hello! This is my custom toast "); Toast toast = new Toast (Getapplicationcontext ());//Set the position in the screen toast.setgravity (gravity.center_vertical, 0, 0);// Set duration toast.setduration (Toast.length_long); Toast.setview (layout); Toast.show (); </span>}});}}

Click on our button to launch our custom toast, which uses the function setgravity (int, int, int) to control the toast position to change its position. The three parameters are: A gravity constant, an offset value in the x direction, and an offset value in the Y direction.

See the results of the operation


It is important to note that most of our toasts are usually prompted at the current focus activity, and if you create a toast message from the service, it will appear above the activity in the current focus.







Let the hint be more personal--Define 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.