2
if (image.getDrawable().getConstantState().equals(getResources().getDrawable(0).getConstantState())){
            image.setVisibility(View.GONE);
        }else{
            image.setVisibility(View.VISIBLE);
        }

That is, if image has not any drawable, image will be GONE, else Visible. But this code does not work

5 Answers5

3

Try below may be it works for you(It's work for me)

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    if (image.getDrawable().getConstantState().equals(image.getContext().getDrawable(R.drawable.shadow_round_white).getConstantState())){

       image.setVisibility(View.GONE);
    }
    else{
       image.setVisibility(View.VISIBLE);
    }

 }
else {
    if (image.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.shadow_round_white).getConstantState())){
       image.setVisibility(View.GONE);
    }

    else{
       image.setVisibility(View.VISIBLE);
    }
 }
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
1

You can call the getDrawable function on the image view and check if it is not null - https://stackoverflow.com/a/16445086/1649353

Community
  • 1
  • 1
Ambuj Kathotiya
  • 369
  • 4
  • 18
0
if (image.getDrawable().getConstantState().equals
            (getResources().getDrawable(R.drawable.your_drawable).getConstantState()){//set here your drawable name(your_drawable)
            image.setVisibility(View.VISIBLE);
        }else{
            image.setVisibility(View.GONE);
        }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

in fact, there is another way to compare:

if(imageView.getDrawable().getConstantState().equals
            (getResources().getDrawable(/*Your drawable*/).getConstantState()))
imageView.setVisibility(View.VISIBLE);
        else
            imageView.setVisibility(View.GONE);
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
0
    if(imageview.getDrawable()==null)
    {
      //if Image View is Null 
    }

check source