26

What is the android equivalent of the CSS style attribute display:none?

In web pages the display:none stylesheet causes the web page element to which it is applied to be hidden and not occupy any space. When the element's attribute is changed dynamically using Javascript to visible, the browser shows the element moving any other elements surrounding it.

3 Answers 3

44

you can use setVisibility on a view. There are 3 options available
VISIBLE - seen by the user
INVISIBLE - not seen by the user. But the view still takes some space.
GONE - not seen by the user, and the view doesn't take up any space.


Adding examples

4 Text views in a LinearLayout oriented vertically, all visible

All visible text views code All visible text views on dev

4 Text views in a LinearLayout oriented vertically, 2 set to invisible 2 text views invisible code 2 text views invisible on dev

4 Text views in a LinearLayout oriented vertically, 2 set to gone 2 text views gone code 2 text views gone on dev

1
  • 1
    @T.Todua added example code, with a screenshot of how they would look on a device
    – bluefalcon
    Commented Jan 13, 2017 at 10:32
11

android:visibility="gone"

or in code: [element].setVisibility(View.GONE);

This is the equivalent of display:none in CSS.

There's also android:visibility="invisible" or .setVisibility(View.INVISIBLE) which is the equivalent of visibility:hidden in CSS - i.e. the element is hidden but still takes up space in the layout.

setVisibility Android docs

android:visibility docs

1
  • Should be android:visibility="gone" not android:visibilty="gone" Commented Feb 20, 2014 at 8:07
3

You can use:

button.setVisibility(view.GONE);

Also

button.setVisibility(view.INVISIBLE);
button.setVisibility(view.VISIBLE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.