Adding image to Toast?

后端 未结 8 1853
梦毁少年i
梦毁少年i 2020-12-02 05:53

Is it possible to programmatically add an image to a toast popup?

相关标签:
8条回答
  • 2020-12-02 06:33

    Simply, Use the following:

    Toast toast = new Toast(myContext);
    ImageView view = new ImageView(myContext); 
    view.setImageResource(R.drawable.image_icon); 
    toast.setView(view); 
    toast.show();
    
    0 讨论(0)
  • 2020-12-02 06:33

    Knickedi's solution is good, but if you only need an icon next to the text you can make use of the fact that the Toast has a pre-defined TextView with the same ID and set the icon on the TextView:

    Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
    if (null!=tv) {
        tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
        tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
    
    0 讨论(0)
  • 2020-12-02 06:37
    Toast aa = Toast.makeText(getBaseContext(), "OPEN",Toast.LENGTH_SHORT);
    ImageView cc = new ImageView(getBaseContext());
    cc.setImageResource(R.drawable.a);
    aa.setView(cc);
    aa.show();
    
    0 讨论(0)
  • 2020-12-02 06:40

    There's always the possibility to create a custom layout. There was one fact which I disliked about that: It breaks the system default toast UI. This could differ on different platforms and implementations. There's no simple way to use the system default resource so I decided to hack the toast and force an image into it.

    Hint: You can get the default resource like this:
    Toast.makeToast(context, "", 0).getView().getBackground()


    Here's a helper which will display an image in front of the toast message: Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()

    I use that to indicate success, info or error. Makes a toast information nicer and more expressive...

    (It's worth mentioning that the hack bases on the fact that the internal toast is using a LinearLayout so isn't system and implementation independent. See comments.)

    public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) {
        Toast toast = Toast.makeText(context, text, length);
    
        View rootView = toast.getView();
        LinearLayout linearLayout = null;
        View messageTextView = null;
    
        // check (expected) toast layout
        if (rootView instanceof LinearLayout) {
            linearLayout = (LinearLayout) rootView;
    
            if (linearLayout.getChildCount() == 1) {
                View child = linearLayout.getChildAt(0);
    
                if (child instanceof TextView) {
                    messageTextView = (TextView) child;
                }
            }
        }
    
        // cancel modification because toast layout is not what we expected
        if (linearLayout == null || messageTextView == null) {
            return toast;
        }
    
        ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
        ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL;
    
        // convert dip dimension
        float density = context.getResources().getDisplayMetrics().density;
        int imageSize = (int) (density * 25 + 0.5f);
        int imageMargin = (int) (density * 15 + 0.5f);
    
        // setup image view layout parameters
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
        imageParams.setMargins(0, 0, imageMargin, 0);
        imageParams.gravity = Gravity.CENTER_VERTICAL;
    
        // setup image view
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageResId);
        imageView.setLayoutParams(imageParams);
    
        // modify root layout
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(imageView, 0);
    
        return toast;
    }
    
    0 讨论(0)
  • 2020-12-02 06:44
        class CustomToast extends AppCompatActivity {
        Button custom_toast;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_toast);
    
            custom_toast = (Button) findViewById(R.id.customToast);
            custom_toast.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater=getLayoutInflater();
                    View layout=inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
    
                    TextView toastTextView=(TextView) findViewById(R.id.toastTextView);
                    ImageView toastimageview=(ImageView) findViewById(R.id.toastImageView);
                    toastTextView.setText("Custom toast in android");
                    toastimageview.setImageResource(R.drawable.ic_launcher_background);
    
                    Toast toast=new Toast(CustomToast.this);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    toast.show();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:49

    Yes, you can add imageview or any view into the toast notification by using setView() method, using this method you can customize the Toast as per your requirement.

    Here i have created a Custom layout file to be inflated into the Toast notification, and then i have used this layout in Toast notification by using setView() method.

    cust_toast_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/relativeLayout1"
      android:background="@android:color/white">
    
        <TextView
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textView1" android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="PM is here"
            android:gravity="center"
            android:textColor="@android:color/black">
        </TextView>
    
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:src="@drawable/new_logo"
            android:layout_below="@+id/textView1"
            android:layout_margin="5dip"
            android:id="@+id/imageView1">
        </ImageView>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="This is the demo of Custom Toast Notification"
            android:gravity="center"
            android:layout_below="@+id/imageView1"
            android:textColor="@android:color/black">
        </TextView>
    
    </RelativeLayout>
    

    CustomToastDemoActivity.java

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.cust_toast_layout, 
        (ViewGroup)findViewById(R.id.relativeLayout1));
    
    Toast toast = new Toast(this);
    toast.setView(view);
    toast.show();
    
    0 讨论(0)
提交回复
热议问题