I am setting an image to ImageView in android code not in xml, but could not make out how to check whether that image has been set in or not in java.
Tried with
Note that if you set an image via ImageView.setImageBitmap(BITMAP)
it internally creates a new BitmapDrawable
even if you pass null
. In that case the check imageViewOne.getDrawable() == null
is false anytime. To get to know if an image is set you can do the following:
private boolean hasImage(@NonNull ImageView view) {
Drawable drawable = view.getDrawable();
boolean hasImage = (drawable != null);
if (hasImage && (drawable instanceof BitmapDrawable)) {
hasImage = ((BitmapDrawable)drawable).getBitmap() != null;
}
return hasImage;
}