is there any way to find resource Id of drawable

Farhan  picture Farhan · Jul 7, 2011 · Viewed 42.9k times · Source

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?

Answer

PIR FAHIM SHAH picture PIR FAHIM SHAH · Jun 1, 2012

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"