Share the custom use of toast in Android _android

Source: Internet
Author: User

1.Toast Source Analysis

The old rules, we first go to see the source code of toast.

Toast there are two ways to display layout, one of the most common calls Toast.makeText()  , see the source code is written in this way

public static Toast Maketext (context context, charsequence text, @Duration int Duration) {
Toast result = new Toast (CO ntext);

Layoutinflater inflate = (layoutinflater)
context.getsystemservice (context.layout_inflater_service);
View v = inflate.inflate (com.android.internal.r.layout.transient_notification, null);
TextView TV = (TextView) V.findviewbyid (com.android.internal.r.id.message);
Tv.settext (text);

Result.mnextview = V;
result.mduration = Duration;

return result;
}

Transient_notification This layout file code is like this

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" match_parent "
android:orientation=" vertical "
android:background="? Android:attr/toastframebackground ">

<textview
android:id=" @android: Id/message "
android: Layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:layout_weight= "1"
Android:layout_gravity= "Center_horizontal"
android:textappearance= "@style/textappearance.toast"
Android:textcolor= "@color/bright_foreground_dark"
android:shadowcolor= "#BB000000"
android: shadowradius= "2.75"
/>

</LinearLayout>

Then we want to modify the toast text message style, in fact, is to modify the toast root layout and messages this textview.

Another display mode for toast is a custom layout display. This method does not call the Toast.makeText() method, but rather the new Toast object, and then calls the setView() method. Of course, the custom layout will not load transient_notification the layout.

2. Implement custom Toast

Let's look at the tool class Toastutil I'm encapsulating.

Import Android.content.Context;
Import Android.view.View;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;

Import Android.widget.Toast;
 /** * Created by Zhao Chen in 2016/8/11.

* * Public class Toastutil {private Toast Toast; private LinearLayout Toastview; /** * Modify the original layout toast/public Toastutil () {}/** * Fully custom layout toast * @param context * @param view/Public Toastutil (
  Context context, View View,int duration) {toast=new toast (context);
  Toast.setview (view);
Toast.setduration (duration);  /** * Add Custom View * to toast @param view * @param postion * @return * * Public toastutil AddView (view View,int postion)
  {Toastview = (linearlayout) Toast.getview ();

  Toastview.addview (view, postion);
return this; /** * Set toast font and background color * @param messagecolor * @param backgroundcolor * @return */public Toastutil Settoastcolor
  T messagecolor, int backgroundcolor) {View view = Toast.getview (); if (view!=null) {TextView message= (TextView) View.findviEwbyid (Android.
    R.id.message));
    Message.setbackgroundcolor (BackgroundColor);
  Message.settextcolor (Messagecolor);
return to this; /** * Set Toast font and background * @param messagecolor * @param background * @return * * public toastutil settoastbackground (int
  Messagecolor, int background) {View view = Toast.getview (); if (view!=null) {TextView message= (TextView) View.findviewbyid (Android.
    R.id.message));
    Message.setbackgroundresource (background);
  Message.settextcolor (Messagecolor);
return to this; /** * Short time display toast */public toastutil (context, charsequence message) {if (toast==null| | (Toastview!=null&&toastview.getchildcount () >1))
    {toast= Toast.maketext (context, message, toast.length_short);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (Toast.length_short);
return to this; /** * Short time display toast */public toastutil (context, int message) {if (toast==null| | (Toastview!=null&&toaStview.getchildcount () >1)) {toast= Toast.maketext (context, message, toast.length_short);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (Toast.length_short);
return to this; /** * Long time display toast/public toastutil long (context, charsequence message) {if (toast==null| | (Toastview!=null&&toastview.getchildcount () >1))
    {toast= Toast.maketext (context, message, toast.length_long);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (Toast.length_long);
return to this; /** * Long time display toast * * @param context * @param message/Public toastutil long (context, int message) {if (toast==null| | (Toastview!=null&&toastview.getchildcount () >1))
    {toast= Toast.maketext (context, message, toast.length_long);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (Toast.length_long);
return to this; /** * Custom Display toast Time * * @param context * @param message
 * @param duration */public toastutil indefinite (context, charsequence, int duration) {if (Toast==nul l| | (Toastview!=null&&toastview.getchildcount () >1))
    {toast= Toast.maketext (context, message,duration);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (duration);
return to this; /** * Custom Display toast Time * * @param context * @param message * @param duration/Public toastutil indefinite (context C Ontext, int message, int duration) {if (toast==null| | (Toastview!=null&&toastview.getchildcount () >1))
    {toast= Toast.maketext (context, message,duration);
  Toastview=null;
    }else{toast.settext (message);
  Toast.setduration (duration);
return to this;

  /** * Display Toast * @return * * * public toastutil Show () {toast.show ();
return this; /** * Get Toast * @return/Public Toast gettoast () {return Toast;}}

The use method for modifying the toast background color is as follows:

Toastutil toastutil=new toastutil ();
Toastutil.short (mainactivity.this, custom message font, background color). Settoastcolor (Color.White, Getresources (). GetColor ( r.color.coloraccent)). Show ();


Modify Toast Background color

The square toast looks a little stiff, and I've customized a background called Toast_radius.xml , and the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"Http://schemas.android.com/apk/res" /android "
android:shape=" Rectangle >
<!--fill color--> <solid android:color=
"#ffc107"/>

<!--android:radius arc radius-->
<corners android:radius= "20dip"/>

</shape>

The code that sets the background above is then changed to:

Toastutil.short (mainactivity.this, "Custom message font color and background"). Settoastbackground (Color.white,r.drawable.toast_radius) . Show ();


Modified Toast of the background

Although the official thought that toast and snackbar should be the form of short text, can not contain the icon, but the personal feeling plus the icon is quite fun ...

To add an icon to the toast, you can do this:

 ImageView toastimage = new ImageView (Getapplicationcontext ());
 Toastimage.setimageresource (r.mipmap.ic_launcher);
 Toastutil.short (Mainactivity.this, "add a ImageView to toast"). Settoastbackground (Color.white,r.drawable.toast_ RADIUS). AddView (toastimage,0). Show ();


Add an icon to the toast

If you want to toast display a custom layout, you can do this:

 View view = Layoutinflater.from (Mainactivity.this). Inflate (r.layout.image,null);
 New Toastutil (Mainactivity.this,view,toast.length_short). Show ();


Custom layout toast, there is only one default icon in my layout file ImageView

As we all know, when the toast method is triggered continuously, the show() toast will be lined up to show the line continuously, feeling not very friendly. So I first judged if toast was not created or if it was added to the view, and if so, to regenerate a Toast object, and if not, just change the message text and display the time.


Toast layout modification, no queued display

Summarize

My tool class is not a complete body, you will be based on the specific needs of their own projects to modify. The above is the toast of Android in the use of all the content, interested in the small partners to quickly start their own practice it.

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.