Quick Tip — How to set the visibility of a button

Xavier Jouvenot
1 min readMay 29, 2020

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to set the visibility of a button.

Self promotion: You can find other articles on Android development on my website 😉

Statically

To specify the visibility of a Button, there are several solutions and the first one we are going to specify a default visibility for a Button.

To do so, we are going to modify the Button XML definition, and add one attribute to it. This attribute is android:visibility and tell if you want the button to be visible are not. By default, this attribute is set to visible.

Here is how such Button XML definition looks like:

<Button
<!--Some attributes-->
android:visibility=invisible
<!--Some other attributes -->
/>

Dynamically

Specifying the attribute in the XML is great! But if you want to modify the visibility of a EditText while the program is running, then, you must use some Java code and the method setVisibility. Here is how it looks like:

Button button = findViewById(R.id.buttonId);
button.setVisibility(View.INVISIBLE);

Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Originally published at http://10xlearner.com on May 29, 2020.

--

--