Android Custom Toast (widget development) sample _android

Source: Internet
Author: User

1, Toast Control:

By looking at the source code, it is found that the principle inside toast is to obtain a View object (TextView) by obtaining a Layoutinflater layout manager through a service context.layout_inflater_service. Set the content to display it

Copy Code code as follows:

public static Toast Maketext (context context, charsequence text, int duration) {
Toast result = new Toast (context);

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;
}

To define a layout file:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "200dip"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >
<imageview
Android:id= "@+id/iv_my_toast"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/notification"/>
<textview
Android:id= "@+id/tv_my_toast"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "18SP"
android:text= "Text"
/>
</LinearLayout>

Custom Mytoast class:

Copy Code code as follows:

public class Mytoast {

/**
* Show Custom Toast
* @param context
* @param ID of the iconID icon
* @param text displayed
*/
public static void Showtoast (context context,int iconID, String text) {
View view = view.inflate (context, r.layout.my_toast, null);
TextView TV = (TextView) View.findviewbyid (r.id.tv_my_toast);
ImageView IV = (ImageView) View.findviewbyid (r.id.iv_my_toast);
Iv.setimageresource (iconID);
Tv.settext (text);
Toast Toast = new Toast (context);
Toast.setduration (0);
Toast.setview (view);
Toast.show ();
}
}

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.