is there any way to find resource Id of drawable

44,705

Solution 1

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 the resource ids being used somewhere in your code.

For example, you can have an array of imageviews with a corresponding array of int that contains the resource id for each imageview

Then, whenever you want to change the image, loop through the array and see what the id is.

Solution 2

This one is the best method to find out the R.drawable.img1 value when u click on the ImageView in your program. The method is something like that in main program. First of all save the image value in the tag like that

public...activity 
{
    //-----this resource name is retrieved through the tag value of the drawable of (touched) ImageView //image ontouchlistener event...
    ImageView imgview1.setTag("img1"); //as of R.drawable.img1
    ImageView imgview2.setTag("img2"); //as of R.drawable.img2

    onTouchListnener event... on imageView
    {    
        Object tag = imageView.getTag();                    
        int id = getResources().getIdentifier( tag, "drawable", this.getPackageName() );
        switch(id)
        {
            case R.drawable.img1:
                //do someoperation of ur choice
                break;
            case R.drawable.img2:
                //do someoperation of ur choice
                break:
        }//end of switch

    }//end of touch listener event

}//end of main activity

"PIR FAHIM SHAH/kpk uet mardan campus"

Solution 3

Create a custom imageview, the rest is simple.

public class CustomImageView extends ImageView {

    private int resID;

    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageResource(int resId) {
        this.resID = resId;
        super.setImageResource(resId);
    }

    public int getResourceId() {
        return resID;
    }
}
Share:
44,705

Related videos on Youtube

Farhan
Author by

Farhan

Updated on July 09, 2022

Comments

  • Farhan
    Farhan almost 2 years

    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?

    • CommonsWare
      CommonsWare almost 13 years
    • ahodder
      ahodder almost 13 years
      I'm not sure what you mean. Why can't you just go back through the code and change it?
    • Farhan
      Farhan almost 13 years
      @AedonEtLIRA: I mean that I have a imageview with a play image, however, when I swipe the row, the play image changes into delete image. So, depending upon what image is set at the moment, I want to perform actions. That is why I want to know what image is being used so that I can perform the action accordingly.. Any clues???
  • Farhan
    Farhan almost 13 years
    I can't use the above method because I don't want to use imageName.. because imageName is what I am looking for. Getting the Id of an image in the resource section is not a problem. The problem is how to know what image is currently in use.. I hope you understand my question.. any help??
  • Senthil Mg
    Senthil Mg almost 13 years
    have you tried like this below... imageView.getId() here imageview object reference of your Imageview class.
  • Kevin Crain
    Kevin Crain almost 8 years
    I am trying to write a test to determine the resource id of a drawable set on an ImageView, do you know of any solution?