Quick Tip — How to set the visibility of a button

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 , there are several solutions and the first one we are going to specify a default visibility for a .

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

Here is how such 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 while the program is running, then, you must use some Java code and the method . 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.