DEV Community

10x learner
10x learner

Posted on • Originally published at 10xlearner.com on

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 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 --> />
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Interesting links

Top comments (0)