ViewStub         

            A ViewStub is a dumb and lightweight view. It has no dimension, it does not draw anything and does not participate in the layout in any way. This means a ViewStub is very cheap to inflate and very cheap to keep in a view hierarchy. A ViewStub can be best described as a lazy include. The layout referenced by a ViewStub is inflated and added to the user interface only when you decide so. 

What is the difference between ViewStub and <include /> tag ?

                     The < include /> will just include the xml contents in your base xml file as if the whole thing was just a single big file. It’s a nice way to share layout parts between different layouts.The < ViewStub /> is a bit different because it is not directly included, and will be loaded only when you actually use it/need it, i.e, when you set it’s visibility to “true”. This a nice optimization because you could have a complex layout with tons of small views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it’ll be loaded.
 

Example:

ViewStubTestActivity.java:

[sourcecode language=”java”]
public class ViewStubTestActivity extends Activity implements OnClickListener {

/**
* Reference to {@link ViewStub}
*/
private View stub;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(this);
}

@Override
public void onClick(View v) {
new LazyLoading().execute();
}

public class LazyLoading extends AsyncTask {
@Override
protected void onPreExecute()
{
if (stub == null) {
stub = ((ViewStub) findViewById(R.id.vsHeader)).inflate();
((ProgressBar) stub.findViewById(R.id.progressBar1))
.setIndeterminate(true);
} else {
stub.setVisibility(View.VISIBLE);
}
}

@Override
protected Void doInBackground(Void… params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
stub.setVisibility(View.GONE);
}

}
}

[/sourcecode]

Main.xml:

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" ></p>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" /></p>
<ViewStub
android:id="@+id/vsHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/inflatedview"
android:layout="@layout/stub" /></p>
</LinearLayout>

[/sourcecode]

stub.xml:

[sourcecode language=”xml”]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" ></p>

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
/>
</LinearLayout>

[/sourcecode]

Note: ViewStub can’t be used more than once.

View stub screenshot.

References:http://www.technotalkative.com/android-viewstub-example/http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-with.html
ViewStub in Android .
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *