2

Using intents to click on an element from the RecyclerView, with a small image on each element, to go to another screen/activity which will show the enlarge picture.

For example, Textview has setText and getText.

How about ImageView? They have setImageResource but I am trying to now get the image resource

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29
  • what if you use imageButton? – Tara Aug 17 '18 at 04:57
  • try this https://stackoverflow.com/questions/26370993/how-to-get-image-resource – Tara Aug 17 '18 at 04:58
  • If one of the answers has helped you solve your issue, please be sure to select it as the correct answer for anyone else who may have the same issue in the future. Thanks and good luck! – Randall Arms Aug 20 '18 at 05:36

2 Answers2

0

There is no method that will give you ImageResource.

You can check ImageView source code. They just convert your ImageResource to Drawable and use it. So you have a getter method for Drawable imageView.getDrawable();. This is only what you can get. There is no getter for getting resource id from Drawable object.

 if (mResource != 0) {
            try {
                d = mContext.getDrawable(mResource);
            } catch (Exception e) {
                Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
                // Don't try again.
                mResource = 0;
            }
        }

Solution 1 From ref

If you want pass Image to another Activity, you can do this.

When you set ImageView resource in Adapter then set a Tag on this.

imageView0.setTag(R.drawable.apple);

Now when you want get that id when user click, then only do

private int getDrawableId(ImageView iv) {
    return (Integer) iv.getTag();
}

Solution 2

If you are one setting ImageView resource, then you would have this resource integer in your List?, so when user click then get resource again like

yourList.get(getAdapterPosition).getImageId();
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

You can programmatically set your first ImageView's tag to the resource id, and then retrieve it later with getTag() to set as the resource id of the new ImageView.

For instance:

//First set your original image tag...
ImageView original; //The original ImageVew
Integer resId = R.drawable.resource; //The original resource
original.setTag(resId); //Add your resource id to the tag

//Then, to get the resource id...
Integer resId = original.getTag();
Randall Arms
  • 407
  • 1
  • 5
  • 22