Show and Hide Views

You can set the visibility of any of your views to make them appear and disappear.

Example uses: 

  • Make some text fields appear after user taps “Get Started” on the opening screen
  • Make a “Welcome” message disappear after the user taps it
  • Make an error message appear when the user does something incorrect
  • Make an image appear or disappear

Example Code

To make this work, you have to create your View as an object in Java. Example:

//declare a View in your class (not inside a method)
TextView myText;

//instantiate View in your onCreate() method
//replace "viewID" with the ID from your layout
myText = findViewById(R.id.viewID);

Making a View invisible:

myText.setVisibility(View.INVISIBLE);

Make the View invisible and also not take up space in the layout:

myText.setVisibility(View.GONE);

Make the View visible:

myText.setVisibility(View.VISIBLE);