Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way to find resource Id of drawable

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?

like image 375
Farhan Avatar asked Jul 07 '11 17:07

Farhan


People also ask

How do you find a drawable ID?

drawable id in the view's id: use v. setId() . Then get it back with v. getId() .

What is the use of resource ID in android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.


3 Answers

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"

like image 135
PIR FAHIM SHAH Avatar answered Oct 02 '22 20:10

PIR FAHIM SHAH


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.

like image 25
f20k Avatar answered Oct 02 '22 19:10

f20k


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;
    }
}
like image 29
Bojan Kseneman Avatar answered Oct 02 '22 20:10

Bojan Kseneman