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

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - 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() { > >}

Solution 2 - Android

Note that if you set an image via ImageView.setImageBitmap(BITMAP)it internally creates a new BitmapDrawableeven if you pass null. In that case the check imageViewOne.getDrawable() == nullis 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;
}

Solution 3 - Android

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

Solution 4 - Android

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.

}

Solution 5 - Android

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.

Solution 6 - Android

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

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMukundaView Question on Stackoverflow
Solution 1 - AndroidPC.View Answer on Stackoverflow
Solution 2 - AndroidNormanView Answer on Stackoverflow
Solution 3 - AndroidJorgesysView Answer on Stackoverflow
Solution 4 - AndroidDavid KariukiView Answer on Stackoverflow
Solution 5 - AndroidRazView Answer on Stackoverflow
Solution 6 - AndroidJay3421View Answer on Stackoverflow
Solution 7 - Androidmohan chaudhariView Answer on Stackoverflow