Android Kotlin .visibility
Taking advantage of some of Kotlin's language features, I use these two extension methods on View
that toggle the visibility with a boolean as a convenience.
fun View.showOrGone(show: Boolean) {
visibility = if(show) {
View.VISIBLE
} else {
View.GONE
}
}
fun View.showOrInvisible(show: Boolean) {
visibility = if(show) {
View.VISIBLE
} else {
View.INVISIBLE
}
}
Basic usage:
imageView.showOrGone(true) //will make it visible
imageView.showOrGone(false) //will make it gone
Although if you are looking for just a little syntactic Kotlin sugar to make your View
visible, you could just write an extension function like so to make it visible.
fun View.visible() {
visibility = View.Visible
}
Basic usage:
imageView.visible()
View.VISIBLE
Should go after the = sign to make the value visible. It has integer constant value in View class. You can check it by pressing ctrl + click (Windows) or cmd + click (Mac).
So it should be like this.
imageView.visibility = View.VISIBLE
Use View.VISIBLE
. That is a constant defined in View
class.
fun hacerVisibleLaFoto(v: View) {
imageView.visibility = View.VISIBLE;
}
Android has static constants for view visibilities. In order to change the visibility programmatically, you should use View.VISIBLE
, View.INVISIBLE
or View.GONE
.
Setting the visibility using myView.visibility = myVisibility
in Kotlin is the same as setting it using myView.setVisibility(myVisibility)
in Java.
In your case:
fun hacerVisibleLaFoto(v: View) {
imageView.visibility = View.VISIBLE
}