How to Check Which Current Image Resource Is Attached to Imageview in Android Xml

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

How to check which image is set on an image view in Android?

You can add tag to ImageView,

Set a tag either in xml or dynamically
In xml:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageview"
android:background="@drawable/drawable_name"
android:tag="drawable_name"/>

Dynamically,

ImageView imageView=(ImageView)findViewById(R.id.imageview);
imageView.setTag("bg");

Use imageView.getTag() to retrieve image information.

String backgroundImageName = String.valueOf(imageView.getTag()); 

you can set tag when you change your drawable and get it by this method

You can see these question to get some other idea

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

How to check which image is linked to ImageView in android?

Check what image is displayed in an ImageView?

Unless you have called mutate() on either of the drawables, you can check if two drawables represent the same image by comparing their ConstantState fields.

ConstantState mine = bun_picture.getConstantState();
ConstantState other = ContextCompat.getDrawable(context, R.drawable.rawbun).getConstantState();

if (mine.equals(other)) {
// they are the same
}

how to check if an ImageView is attached with image in android

imageViewOne.getVisibility() == 0

use this instead:

imageViewOne.getDrawable() == null

From documentation:

/**

* Gets the current Drawable, or null if no Drawable has been

* assigned.

*

* @return the view's drawable, or null if no drawable has been

* assigned.

*

*/

public Drawable getDrawable() {

}

How to get Image Resource

I think you can't, current API doesn't allow this.
But if you really need this, you can do something like this:

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);
//
// somewhere later...
//
Integer resource = (Integer)imageButton.getTag();

Get the ID of a drawable in ImageView

I think if I understand correctly this is what you are doing.

ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());

switch(getDrawableId(imageView)) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
break;
}
});

Right? So that function getDrawableId() doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags

ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.someImage == imageView.getId());

// See here
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;

switch(integer) {
case R.drawable.foo:
imageView.setDrawableResource(R.drawable.bar);
imageView.setTag(R.drawable.bar);
break;
case R.drawable.bar:
default:
imageView.setDrawableResource(R.drawable.foo);
imageView.setTag(R.drawable.foo);
break;
}
});

How to check image resource of an imageview programmatically?

When you set the background drawable also do set a tag to the imageview

imageView.setTag(R.drawable.demo);

And then compare

if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}


Related Topics



Leave a reply



Submit