23

I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)

but what I want to do is to check the current ImageSource before changing it.

basically, I want to implement my own group radio buttons using imageViews. so every time any imageView is clicked, the oncliked event method will change the Image Resources of my group.

and sorry for my poor English.

regards, redsea

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
Red Sea
  • 243
  • 1
  • 2
  • 5

7 Answers7

34

There is no getDrawableId function so you'll need to do something like set a tag for the ImageView when you change its drawable. For instance, set the drawable id as a tag for the ImageView so you could just get the drawable id from the tag.

How to do that?

I'd say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:

myImageView.setTag(R.drawable.currentImage);    //When you change the drawable
int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id

What if I already have a tag on my view

Android views can host multiple tags at the same time, as long as they have a unique identifier. You'd need to create a unique id resource and add it as the first argument to the setTag method call. Leaving the code like this:

myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
2

To get Image source, you can use Drawable.ConstantState. For example, in my problem I was implementing a button click to change the ImageView. I created two Drawable.ConstantState objects and compared them. The code is given below:

ImageView myImg=(ImageView) findViewById(R.id.myImg);
        Drawable.ConstantState imgID = myImg.getDrawable().getConstantState();
        Drawable.ConstantState imgID2 = getDrawable(R.drawable.otherImage).getConstantState();
        if(imgID!=imgID2)
        {
            // Block of Code
        }
        else // Other Block of Code 
090
  • 97
  • 10
1

Are you saying that you intend to check the resource id to determine if a radio button is selected or not? While this could probably work, it isn't a very good design. Generally speaking you want to separate the model from the view (otherwise known as the Model-View-Controller paradigm). To do this, you might have your radio button group maintain an index for the selected item, and have each of your items stored in a list. When an item is selected, you can determine its index, update the group's model, and then update the UI to reflect that change.

Its not that what you're proposing wont work, it just isn't good design and leads to poor maintainability (such as if the resource names change).

mtmurdock
  • 12,756
  • 21
  • 65
  • 108
1

Actualy after my try ,I find there is a way to get source of imageView without need of tag property.

Integer src = attrs.getAttributeResourceValue(
                    "http://schemas.android.com/apk/res/android", "src", -1);

The first parameter is namespace of android and src is the property name. This method return the image resource id if it found or it will return -1;

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Andrew Carl
  • 802
  • 1
  • 8
  • 15
  • You didn't mention two very important things: how to get **attrs** and how to select the exact ImageView that you wand to get **src**? – dragi Oct 24 '14 at 11:31
1

If you want get the src drawable, use the following method of ImageView : http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()

Mickey Tin
  • 3,408
  • 10
  • 42
  • 71
0

You can use this.

 private Drawable colocaImgen(String nombreFile) {
    Drawable res1 = null;
    String uri1 = null;
    try {
        //First image
        uri1 = "@drawable/" + nombreFile;
        int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
        res1 = getResources().getDrawable(imageResource1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        //int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
        res1 = getResources().getDrawable(R.drawable.globo2);// Default image
    }
    return res1;
}

Then

((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
0

I had same problem and here is my working solutions:

public void engLayoutExpand(View view) {
    ImageView iv = (ImageView) view;

    // One way:
    Bitmap bm1 = ((BitmapDrawable) iv.getDrawable()).getBitmap();
    Bitmap bm2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.baseline_expand_less_black_36)).getBitmap();

    if (bm1 == bm2) {
        iv.setImageResource(R.drawable.baseline_expand_more_black_36);
    }

    // Other way
    Drawable.ConstantState cs1 = iv.getDrawable().getConstantState();
    Drawable.ConstantState cs2 = getResources().getDrawable(R.drawable.baseline_expand_less_black_36).getConstantState();

    if ( cs1 == cs2) {
        iv.setImageResource(R.drawable.baseline_expand_more_black_36);
    }

}
Abigail La'Fay
  • 749
  • 1
  • 9
  • 18