Can I hide the image button on the layout (dimensions and background) until a call appears to set the visibility?

I have a hidden image button in one of my xmls layouts, with the background set to the drawn image. I set the visibility to invisible, as I want the image to be displayed once in a while. The problem is that even if the one being drawn is not displayed, the image button still takes up space - is there a way to hide the background image and make it size 0 until I call it in my main class?

Thanks!

Edit: So, in my xml, how would I write this?

<ImageButton android:id="@+id/myimage" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/my_image"></ImageButton> 

I want the image to always go away, if a certain condition does not arise, I want the image to be visible under this one condition. So in my xml I will need to install GONE, but in my conditional statement I will say something like:

myimage.setVisibility (SHOW) ;?

+4
source share
4 answers

All views, including ImageButton, inherit from android.view.View, so they all have a visibility attribute that can be set. Instead of setting the visibility of a resource that can be downloaded, set it to ImageButton.

+6
source

Try setting visibility to GONE

+14
source

Do not set the width / height to zero, which is ugly. A view will always occupy space unless you change the visibility setting. This is the code you want:

myImageButton.setVisibility (View.GONE);

View.GONE - invisible, takes up View.INVISIBLE - invisible, but still takes up View.VISIBLE - use this to return it

+9
source

Sets Imageview's visibility property like this in java

 imgView.setVisibility(View.VISIBLE); imgView.setVisibility(View.INVISIBLE); imgView.setVisibility(View.GONE); 

Or like this in XML

 android:visibility="visible" android:visibility="gone" android:visibility="invisible" 

The result for each will be like this:

enter image description here

+1
source

All Articles