Raj
Raj

Reputation: 3061

What is the Android equivalent of display:none in CSS?

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.

Upvotes: 26

Views: 20633

Answers (3)

bluefalcon
bluefalcon

Reputation: 4245

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

Upvotes: 45

whostolemyhat
whostolemyhat

Reputation: 3123

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

Upvotes: 11

BrainCrash
BrainCrash

Reputation: 13182

You can use:

button.setVisibility(view.GONE);

Also

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

Upvotes: 3

Related Questions