You can show and hide android views (layout, button, textview etc.,) at any time. You can also use animations like fadeOut or fadeIn to make it more beautiful while hiding and showing the views.

Please refer Android Working with XML Animations if you want to add animations to view while hiding/showing.

Using view.setVisibility(int) function you can show and hide the view by appropriate value.

Method 1

Hide the view using View.GONE

If you use View.GONE value, the element will be removed from the layout hence it won’t occupy any space in the layout.

Button btnSend = findViewById(R.id.btnSend);

// hiding the button
btnSend.setVisibility(View.GONE);
Method 2

Hide the view using View.INVISIBLE

If you use View.INVISIBLE value, the element won’t display but it take it’s space in the layout as the element still be added to the layout.

// hiding the button
btnSend.setVisibility(View.INVISIBLE);

Showing the view

And showing can be done using View.VISIBLE value

// showing the button
btnSend.setVisibility(View.VISIBLE);
Author

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • Arul

    It’s really so nice of all posts.,thank you…..!

  • Nischay Nahata

    Hi Ravi, first of all thanks for writing one of the best series of android tutorials. Do you have a tutorial on animations where the layouts switch on pressing a button (without changing the activity) and it reverts back on pressing the back button

  • Uche Philip

    ANDROID HIVE!!!! AWESOME!!

  • jounes

    Hello Mr Ravi Tamada …can i ask you for hellp!!…

    I have a problem with my android interface, when i created user forms ..i need to display different Spinner …where the seconds spinner it’s depend from the first

    exempl:1- i have the first spinner called “categories”,

    “categories” =item 1
    item 2
    item 3

    2-the second spinner called “type”

    What i need its :when i click in the “item 1” from the first spinner,i don’t need to show the second spinner…and when i click in the “item 2” the second spinner are shown in the interface

    can you sir to give me a idea to program that .

    thanks.