Show pictures in Toast

Source: Internet
Author: User

about how to display a picture in a toast, first customize a toast, customize a layout, this layout you want the toast to show what kind of layout you define, and then put a imageview in the custom layout, plug your own custom layout into the toast, Then call your own custom toast on the go.
This is what I do, although the more ugly can explain the problem.

is a custom toast code:
Package com.rytong.toast;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;

public class Mainactivity extends Activity {

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button btn = (button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
TODO auto-generated Method Stub
Mytoast.mytosat (Mainactivity.this, R.drawable.icon, "show Pictures", Toast.length_long);
}
});
}
}

Package com.rytong.toast;

Import Android.content.Context;
Import Android.graphics.Color;
Import android.view.Gravity;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;

public class Mytoast {
public static void Mytosat (context context, int imageId, String content, int duration) {
New A toast pass in the context of the activity to be displayed
Toast toast = new toast (context);
The time displayed
Toast.setduration (duration);
Where to display
Toast.setgravity (gravity.bottom, 0, 300);
To lay out a toast again
LinearLayout toastlayout = new LinearLayout (context);
Toastlayout.setorientation (linearlayout.horizontal);
Toastlayout.setgravity (gravity.center_vertical);

ImageView ImageView = new ImageView (context);
Imageview.setimageresource (IMAGEID);
Add the ImageView to the Toastlayout layout
Toastlayout.addview (ImageView);

TextView TextView = new TextView (context);
Textview.settext (content);
Textview.setbackgroundcolor (Color.gray);
Add the TextView to the Toastlayout layout
Toastlayout.addview (TextView);
Toastlayout.setbackgroundcolor (Color.gray);
Add Toastlayout to the toast layout
Toast.setview (toastlayout);
Toast.show ();
}
}

Show pictures in 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.