Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to check which image resource is attached to ImageView in xml, I am able to check that which image resource is attached to image view but my requirement is to how to check that the ImageView has the same resource which I have set into xml or not, based on that I need to perform some actions.Code always executes else part. Following is my code,

  if (regProfile.getDrawable() == getResources().getDrawable( R.drawable.ivpic))      {       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();      } 
like image 311
Reena Avatar asked Apr 29 '14 07:04

Reena


People also ask

How do you get a resource image?

You can use imageButton. getDrawable() to get the drawable of the ImageButton object and then use setImageDrawable() instead of setImageResource() .

What is SRC in ImageView?

The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.


1 Answers

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; } 
like image 143
Jitesh Upadhyay Avatar answered Sep 19 '22 12:09

Jitesh Upadhyay