How to check if background color is set on Image view?

0

I want to set background color on Image view if it doesn't already have background color.

I'm trying something like this, but obviously it won't work. How can I set

private ImageView myImage = findViewById(R.id.myimage);

if (myImage.getBackground() != null) {
    myImage..setBackgroundColor(0x00000000)
}
else {
    myImage.setBackgroundColor(Color.rgb(100, 100, 50));
}

This work to set background color and remove it, but only the first time. The problem is that when background is set to 0x00000000 it's transparent, hence not null. Is it possible to set background color to null? Or do I need to set the background as transparent by default and check if it's transparent?

android
asked on Stack Overflow Apr 26, 2019 by janlindso • edited Apr 26, 2019 by Zoe

1 Answer

2

You don't need to setBackgroundColor to transparent. setBackgroundColor will create a ColorDrawable for the background. Since setBackgroundColor requires you to provide it an ColorInt you cannot invoke it with null. What you can do though is to replace setBackgroundColor(0x00000000) with setBackground(null)

answered on Stack Overflow Apr 26, 2019 by Rene Ferrari

User contributions licensed under CC BY-SA 3.0