How to check which current image resource is attached to ImageView in android xml?

Hi please have a try with this as follows

if (regProfile.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ivpic).getConstantState()) 
{
  Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
  // new RegisterAsyntaskNew().execute(); 
} 
else 
{
 Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
 // new RegisterAsyntask().execute(); 
}

please use .getConstantState() to compare

visit

http://developer.android.com/reference/android/graphics/drawable/Drawable.html

http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html

EDIT:

.getResources().getDrawable(imageResource)

Is deprecated in API21, so I changed Jitesh Upadhyay’s answer.

Here is the code:

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static boolean checkImageResource(Context ctx, ImageView imageView,
        int imageResource) {
    boolean result = false;

    if (ctx != null && imageView != null && imageView.getDrawable() != null) {
        ConstantState constantState;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            constantState = ctx.getResources()
                    .getDrawable(imageResource, ctx.getTheme())
                    .getConstantState();
        } else {
            constantState = ctx.getResources().getDrawable(imageResource)
                    .getConstantState();
        }

        if (imageView.getDrawable().getConstantState() == constantState) {
            result = true;
        }
    }

    return result;
}

Leave a Comment