how to get the source of ImageView in order to change it?

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


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).


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 

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