Is there any way to get the Drawable resource ID? For example, I am using an ImageView and I may initially use icon.png as its image but later I may change the image to icon2.png. I want to find out using the code that which image my ImageView is using from the resource. Is there any way?
相关问题
- 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
There are a few steps to this:
create integer-array xml to hold names of drawables (ie: "@drawable/icon1" ... "@drawable/iconN"
use getIdentifier above to get the "array"
with ID for list of drawable, getStringArray will give you an array names of drawables you specified in step 1.
then use any of the drawable name in the array with getIdentifier again to get the drawable ID. This use "drawable" instead of "array" type.
use this ID to set image for your view.
HOpe this will help.
I now that the question is pretty old, but maybe someone will find it useful.
I have a list of TextViews with Drawables and want to set click listeners for all of them, without any need to change the code, when the layout is changed.
So I've put all drawables into a hashmap to get their ids later.
main_layout.xml
MyActivity.java
Method getAllResourceIDs I've used from here
This one is the best method to find out the R.drawablw.image1 value when u click on the ImageView in ur prgoram ,The method is some like that in main program first of all save the image value in the tag like that
Another approach : you just need to create your own customized view. and onCreate. then iterate the AttributeSet object(attrs) to find index of your attribute. then just call getAttributeResourceValue with index, then you will get initial ResouceID value. a simple example of extending ImageView to get ResourceID for background:
This approach is suitable for who is looking to store initial values.. solution provided by Bojan Kseneman (+1 vote) is for keeping ref to resourceID whenever view is changed.
You can get the id of an image with it's name by below code.
Are you trying to determine what the current image is on the
imageview
, in order to change it to some other image?If that's so I suggest doing everything using code instead of xml.
i.e. Use
setImageResource()
to set the initial images during initialization and keep track of theresource ids
being used somewhere in your code.For example, you can have an array of
imageviews
with a corresponding array ofint
that contains theresource id
for eachimageview
Then, whenever you want to change the image, loop through the array and see what the id is.