baekacaek
baekacaek

Reputation: 1557

Difference between setVisibility() and setAlpha()

What is the difference between setVisibility(View.GONE) and setAlpha(0f)?

Upvotes: 7

Views: 10661

Answers (6)

Wision
Wision

Reputation: 61

setVisibility(View.GONE) makes your view invisible and not occupy place (which is different from View.INVISIBLE)

setAlpha(0f) makes your view become transparent but the view still occupy place and can receive some event such as click event

Upvotes: 1

CuriousSuperhero
CuriousSuperhero

Reputation: 6671

I think it would be valuable to define that what is the difference between all 3 different cases below.

Notice that also setVisibility(View.INVISIBLE) and setAlpha(0f) are different.

setVisibility(View.GONE)

Hides the view and removes the space that view occupied. Inactivates the OnClickListener that is bind to view.

setVisibility(View.INVISIBLE)

Hides the view and keeps the space that view occupies. Inactivates the OnClickListener that is bind to view -> if the occupied space is clicked, the event is NOT triggered.

setAlpha(0f)

Hides the view and keeps the space that view occupies. Keeps the OnClickListener active that is bind to view -> if the occupied space is clicked, the event is triggered.

Upvotes: 3

user2319290
user2319290

Reputation: 7

setAlpha() - Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque like (0

Upvotes: 0

TronicZomB
TronicZomB

Reputation: 8747

setVisiblity(View.GONE) makes the View invisible:

This view is invisible, and it doesn't take any space for layout purposes.

setAlpha(0) just makes the View transparent, but it is still in the space and able to be interacted with.

Alpha docs: http://developer.android.com/reference/android/view/View.html#setAlpha(float)

Visibility docs: http://developer.android.com/reference/android/view/View.html#setVisibility(int)

Upvotes: 4

Sunil Kumar
Sunil Kumar

Reputation: 7092

If you need to remove them and then show them again, it could be better to just set visibility to gone and then change back to visible again. setAlpha(0f) is equivalent to setVisibility(View.INVISIBLE) that only hides the view

Upvotes: 1

Marcin S.
Marcin S.

Reputation: 11191

setVisibility(View.GONE) will not only hide your view but it will also recycle the space occupied by this view. However setAlpha(0f) is equivalent to setVisibility(View.INVISIBLE) that only hides the view and still takes the space in your layout.

Upvotes: 11

Related Questions