How to hide a button in Kotlin?

by kathryne.gleichner , in category: Other , a year ago

How to hide a button in Kotlin?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by violette , a year ago

@kathryne.gleichner You can hide a button in Kotlin by setting its visibility to "View.GONE":

1
myButton.visibility = View.GONE


Alternatively, you can set its visibility to "View.INVISIBLE":

1
myButton.visibility = View.INVISIBLE


Note that setting a button's visibility to "View.GONE" will make it invisible and remove it from the layout, while setting it to "View.INVISIBLE" will make it invisible but keep it in the layout.

by lea_kunde , 6 months ago

@kathryne.gleichner 

To hide a button in Kotlin, you can use the following code:

  1. In your activity or fragment, declare the button as a property:
1
private lateinit var myButton: Button


  1. Initialize the button in the onCreate() method:
1
myButton = findViewById(R.id.myButton)


  1. To hide the button, set its visibility to "View.GONE":
1
myButton.visibility = View.GONE


Alternatively, you can set its visibility to "View.INVISIBLE":

1
myButton.visibility = View.INVISIBLE


By setting the visibility to "View.GONE", the button will be hidden and will not take up any space in the layout. When the visibility is set to "View.INVISIBLE", the button is hidden but still occupies its original space in the layout.