How to get id of an Imageview on android?

Asked

Viewed 1,338 times

0

How I can get an id of an image from an Imageview. Example, for me to set an image in my Imageview I use the following code.

imageView.setImageResource(R.drawable.icone_x);

How do I do the reverse process? For example, I want to receive an integer referencing R.drawable.icone_x, more or less like this.

int Resid = imageView.getImageResource();

But there is no such method.

  • Juarez, I don’t quite understand your question. Do you only want to store the Resid of a drawable in a variable? If so, the R.drawable.icone_x already does this job for you!

  • This Imageview is dynamic, as the user clicks it changes image, I created a Reader and need to know which image it is currently showing.

2 answers

2

I’m assuming your intention is to get an integer referencing your image, but that’s not really what happens when you use the method setImageResource. You can check in ImageView#setImageResource() what the Android behind, which is to seek a resource that in most of the case is a BitmapDrawable, but it can be another kind, and then it’s applied to ImageView.

The best solution for your case I believe is to keep the name or the identification itself somewhere to recover, for example using setTag():

imageView.setImageResource(R.drawable.image1);
imageView.setTag("image1");

And then you can get it back like this:

int resId = getResources().getIdentifier(imageView.getTag(), "drawable", getPackageName());

Of course, it is an assumption that depends on how you are wanting to deploy.

1


To reply from @Paulorodrigues is pretty cool. But I leave my suggestion, that would extend the ImageView, adding the behavior you want. Of course it involves a Refactoring if you are already using ImageView's in your application, but it does not involve adding more code (which you may forget) when setting a tag in the ImageView.

The subclass would be:

public class CustomImageView extends ImageView {

    private int mImageResource;

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

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

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs, 0, defStyle);
    }

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

        // Recupera o id do resource setado no xml
        final TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ImageView, defStyleAttr, defStyle);
        mImageResource = a.getResourceId(com.android.internal.R.styleable.ImageView_src, 0);

        a.recycle();
    }

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

    public int getImageResource() {
        return mImageResource;
    }
}

The use in xml would be similar to the others, just changing ImageView for nome.do.seu.pacote.CustomImageView.

  • True, I hadn’t thought of that hiccup. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.