Is it possible to programmatically add an image to a toast popup?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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:
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
CustomToastDemoActivity.java
I think this is better that we show text of Toast on the image which we pass to the makeImageToast function... so I shades Knickedi codes and :
}
and this is use of it:
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.)Simply, Use the following: