Android Custom toast Duration, location, and display view

Source: Internet
Author: User

Android Custom Toast

The first is the custom duration:

Say so, but Android comes with two time length_short (2 seconds) and Length_long (3.5 seconds) basic enough, and generally did not specifically to set a few 10 seconds of toast, so, it is better to directly get a dialog directly.

Let's take a look at how to get toast to customize the display length within 3.5 seconds:

    publicstaticvoidshowShortint duration) {        final Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);        toast.show();        new Handler().postDelayed(new Runnable() {            publicvoidrun() {                toast.cancel();            }        }, duration);    }

As for the custom duration over 3.5 seconds, you can use the dormant thread to implement

    /** * @param msg to display the text message * @param Context passed * @param Duration Custom Duration */     Public Static void Show(Context context, String msg,Final intDuration) {FinalToast Toast = Toast.maketext (context, MSG, toast.length_long);Final LongStartTime = System.currenttimemillis (); Toast.show ();NewHandler (). postdelayed (NewRunnable () { Public void Run() {Try{//thread starts to hibernateThread.Sleep ((Long) (duration)); }Catch(Interruptedexception e)                {E.printstacktrace (); }LongEnttime = System.currenttimemillis (); LOG.I ("Toastutil","------>>> Display time:"+ (enttime-starttime)); }//This 100 milliseconds is only used to make toast from hidden to display the time given, or directly dormant thread, toast is not displayed}, -); }

Length of time shown: 10.191 seconds

This method is basically not used, this is directly let the UI thread sleep Ah, unless the role of some special places, or do not use; if it is still used, it is recommended to make a dialog custom view to clone a toast, so that you want to show how long to show how long.

The next step is to customize the view and customize the location.

The first is the layout file R.layout.toast_view:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:orientation="vertical"android:layout_width="Match_parent"android: Gravity="center"android:background="@null"android:layout_height ="Match_parent">                        <TextView        Android:layout_width="Wrap_content"        Android:id="@+id/tv_message_toast"        Android:textcolor="#fff"        Android:maxwidth="200DP"        android:padding="15DP"        android:textsize="18SP"        android:gravity="Center"        Android:drawableleft="@drawable/btn_delete_red_circle"        Android:background="@drawable/shape_toast_bg"        Android:layout_height="Wrap_content"/></linearlayout>

And then the Toast method.

     Public Static void Show(Context context, String msg) {Toast Toast =NewToast (context);//Set toast display position, center, offset to X and y axes to 0Toast.setgravity (Gravity.center,0,0);//Get a custom viewView view = Layoutinflater.from (context). Inflate (R.layout.toast_view,NULL); TextView tvmessage = (TextView) View.findviewbyid (r.id.tv_message_toast);//Set textTvmessage.settext (msg);//Settings ViewToast.setview (view);//Set Display durationToast.setduration (Toast.length_long);//DisplayToast.show (); }

After the activity is called ToastUtil.show(this, "这是一个自定义视图和位置的Toast"); , the following is displayed:

The layout can be customized, although this toast is a bit ugly, but let everyone know how to get a custom toast.

Android Custom toast Duration, location, and display view

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.