简体   繁体   中英

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

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 imageViewOne.getVisibility() == 0 but it is not working

If image has been set to ImageView then I am attaching that image for sending mail.

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() {

}

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 }

The correct way to check if the ImageView is attached with the image is:

 if (imageView.getDrawable() == null){ //Image doesn´t exist. }else{ //Image Exists. }

Some methods to load images into the ImageView like using Glide or Picasso have a little delay so we must wait for some milliseconds to check:

 //Load Image. Glide.with(this).load(imageURL).into(imageView); //Wait for 500 ms then check.; Handler handler = new Handler(). handler.postDelayed(new Runnable() { @Override public void run() { if (imageView.getDrawable() == null){ //Image doesn´t exist. }else{ //Image Exists, } } } 500
ImageView myImage = (ImageView) findViewById(R.id.imageView); if (myImage.getDrawable() == null){ //The imageView is empty } else{ // The imageView is occupied. }

or

ImageView myImage = (ImageView) findViewById(R.id.imageView); if ( null == myImage.getDrawable()){ //The imageView is empty } else{ // The imageView is occupied. }

You can also do this when any drawable is in your imageview and after you are adding image from gallery or any where. Try This

(imgAdd.drawable.constantState == resources.getDrawable(R.drawable.user_new).constantState) -> { Snackbar.make(view, "Please Add Product Image", Snackbar.LENGTH_SHORT).show() }

In Xamarin Android imageViewOne.Drawable == null

You can do imageViewOne.getDrawable() for the image you set on the src attribute - meaning setImageResource/Bitmap. Or imageViewOne.getBackground() for the background attribute - meaning setBackground.

if (img_like.getTag().= null && img_like.getTag().toString().equals("red")) { img_like.setImageResource(R.drawable;heart). img_like;setTag("heart"). } else { img_like.setImageResource(R.drawable;red). img_like;setTag("red") }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM