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
Adding a Vertical Scrollbar to an Alertdialog in Android
Remove All Items from Recyclerview
How to Hide "Navigation" and "Gps Pointer" Buttons When I Click The Marker on The Android Google Map
Unfortunately App Is Getting Stopped While Checking for Network
Add Drop Shadow Effects to Edittext Field
How to Run The Same Asynctask More Than Once
Bitmapdrawable Deprecated Alternative
How to Check Database on Not Rooted Android Device
How to Get Pid of Android Application Without Using Adb Shell
What Is The Different Between Explicit and Implicit Activity Call in Android
How to Disable Viewpager Adapter on Touching Specific Views
Not Able to Debug App in Android Studio
How to Play an Audio File on a Voice Call in Android
Android, How Do How to Get a List of All Files in a Folder
Hide Tablayout on Scroll of Content Instead of Toolbar
How to Play .Mp4 Video in Videoview in Android
How to Disable "Next" Button on a Edittext Software Keyboard (Replace with "Done" Button)