Is there a difference between ViewStub and View?

I am new to Android development. I read about ViewStub from https://developer.android.com/training/improving-layouts/loading-ondemand.html

He mentions that it is cheap and less memory to use it.

I have the following:

MainLayout.xml

<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" >

.....

.....

<Button android:id="@+id/ShowBackground"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

<Button android:id="@+id/ShowBackground2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"
    android:text="@string/title_close" />

<View
  android:id="@+id/ShowView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:background="@color/blue" />

<ViewStub
    android:id="@+id/ShowViewStub"
    android:layout="@layout/test"        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone" />

</RelativeLayout>

test.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

</RelativeLayout>

MainActivity:

    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);
       }
    });
   }

Questions:

View and ViewStub work fine in MainActivity.

I understand that ViewStub is good for a view that is rarely used. If the ViewStub is called at least 1 time in MainActivity, should it not be more memory usage, since the test.xml layout is added to the activity?

As I see it, the ViewStub is always visible if it is not called View.Gone ....

Can someone explain what are the differences between the two?

I am very grateful for your help,

Thank.

+4
1

include/" > xml xml , . .

ViewStub/" > , , / , .. VISIBLE ( ) INVISIBLE ( , 0). , , - . , .

0

All Articles