title: Is there any difference between ViewStub and View?
body: <p>I am new to Android Development.
  I read about ViewStub from <a href="https://developer.android.com/training/improving-layouts/loading-ondemand.html" rel="nofollow">https://developer.android.com/training/improving-layouts/loading-ondemand.html</a></p>
  
  <p>It mentions that it is cheap and less memory to use it.</p>
  
  <p>I have the following:</p>
  
  <p>MainLayout.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/Layout1"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:background="@color/blue"
      android:orientation="vertical" &gt;
  
  .....
  
  .....
  
  &lt;Button android:id="@+id/ShowBackground"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
  
  &lt;Button android:id="@+id/ShowBackground2"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
  
      android:layout_alignParentBottom="true"
      android:text="@string/title_close" /&gt;
  
  &lt;View
    android:id="@+id/ShowView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@color/blue" /&gt;
  
  &lt;ViewStub
      android:id="@+id/ShowViewStub"
      android:layout="@layout/test"        
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:visibility="gone" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>test.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/ShowViewLayout"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:background="@color/blue_dimmer"
      android:orientation="vertical" &gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>    private ViewStub mViewStub;
      private View mView;
      private Button mBackground1;    
      private Button mBackground2
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.MainLayout);
  
          mView = (ViewStub) findViewById(R.id.ShowView);
          mViewStub = (ViewStub) findViewById(R.id.ShowViewStub);
          mBackground1 = (Button)this.findViewById(R.id.ShowBackground);
  
      mBackground1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           mView.setVisibility(View.VISIBLE);
         }
      });
     }
  
      mBackground2 = (Button)this.findViewById(R.id. ShowBackground2);
      mBackground2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           mViewStub.setVisibility(View.VISIBLE);
         }
      });
     }
  </code></pre>
  
  <p>Questions:</p>
  
  <p>View and ViewStub are working fine in the MainActivity.</p>
  
  <p>I do understand ViewStub is good for the view that is rarely used.
  If ViewStub is called at least 1 time in the MainActivity, shouldn't it be more memory usage since the test.xml layout is added to the activity?</p>
  
  <p>As I can see is that ViewStub is always visible unless it is called View.Gone....</p>
  
  <p>Can someone please explain what the differences between the two?</p>
  
  <p>I greatly appreciate for your help,</p>
  
  <p>Thank you.</p>