is there any way to find resource Id of drawable

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"


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.


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;
    }
}