Skip to main content

Android : How to hide and show a view

Points To Remember

There are three things yo can do to set the visibility of a VIEW in android

  1. VISIBLE - it makes the VIEW visible .
  2. INVISIBLE - it makes the VIEW invisible but it still takes the space occupied by the View element on the screen.
  3. GONE - it makes the VIEW invisible and also hides the space occupied by the view on the screen.

How to make a TextView Visible

The following code makes the TextView invisible to the user and also hides the space occupied by the TextView on screen
<TextView
android:id="@+id/text_view_name"
android:visibility="gone"
android:layout_height="30dp"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:background="#e7e7e1"
xmlns:android="http://schemas.android.com/apk/res/android" />

The following code makes the TextView invisible to the use but does not hide the space occupied by it.
<TextView
android:id="@+id/text_view_name"
android:visibility="invisible"
android:layout_height="30dp"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:background="#e7e7e1"
xmlns:android="http://schemas.android.com/apk/res/android" />

The following code makes the TextView visible to the use and also occupies the space on the screen
<TextView
android:id="@+id/text_view_name"
android:visibility="visible"
android:layout_height="30dp"
android:textColor="@android:color/holo_blue_dark"
android:paddingLeft="15dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:background="#e7e7e1"
xmlns:android="http://schemas.android.com/apk/res/android" />

To set the visibility of this text box from the Activity you can do it as follows.
  • TextView textView = (TextView) findViewById(R.id.text_view_name);
    textView.setVisibility(View.VISIBLE);
  • TextView textView = (TextView) findViewById(R.id.text_view_name);
    textView.setVisibility(View.INVISIBLE);
  • TextView textView = (TextView) findViewById(R.id.text_view_name);
    textView.setVisibility(View.GONE);

Comments