Home | History | Annotate | Download | only in articles
      1 page.title=Layout Tricks: Using ViewStubs
      2 @jd:body
      3 
      4 <p>Sharing and reusing UI components is very easy with Android, thanks to the <a
      5 href="layout-tricks-reuse.html">&lt;include /&gt;</a> tag. Sometimes it's so
      6 easy to create complex UI constructs that your UI ends up with a large number of
      7 views, some of which are rarely used. Thankfully, Android offers a very special
      8 widget called {@link android.view.ViewStub}, which brings you all the benefits
      9 of the <code>&lt;include /&gt;</code> without polluting your user interface with
     10 rarely used views.</p>
     11 
     12 <p>A <code>ViewStub</code> is a dumb and lightweight view. It has no dimension,
     13 it does not draw anything and does not participate in the layout in any way.
     14 This means that a <code>ViewStub</code> is very cheap to inflate and very cheap
     15 to keep in a view hierarchy. A <code>ViewStub</code> can be best described as a
     16 <em>lazy include</em>. The layout referenced by a <code>ViewStub</code> is
     17 inflated and added to the user interface only when you decide so.</p>
     18 
     19 <p>The following screenshot comes from the <a
     20 href="http://code.google/p/shelves">Shelves</a> application. The main purpose of
     21 the activity shown in the screenshot is to present the user with a browsable
     22 list of books:</p>
     23 
     24 <img style="margin: 0px auto 10px; display: block; text-align: center;" src="images/viewstub1.png" alt="" id="BLOGGER_PHOTO_ID_5314039375336055346" border="0">
     25 
     26 <p>The same activity is also used when the user adds or imports new books.
     27 During such an operation, Shelves shows extra bits of user interface.
     28 The screenshot below shows the progress bar and cancel button that
     29 appear at the bottom of the screen during an import:</p>
     30 
     31 <img style="margin: 0px auto 10px; display: block; text-align: center;" src="images/viewstub2.png" alt="" id="BLOGGER_PHOTO_ID_5314039800002559378" border="0">
     32 
     33 <p>Because importing books is not a common operation, at least when compared to
     34 browsing the list of books, the import panel is originally represented
     35 by a <code>ViewStub</code>:</p>
     36 
     37 <img style="margin: 0px auto 10px; display: block; text-align: center;" src="images/viewstub3.png" alt="" id="BLOGGER_PHOTO_ID_5314040334008167378" border="0">
     38 
     39 <p>When the user initiates the import process, the <code>ViewStub</code> is 
     40 inflated and replaced by the content of the layout file it references:</p>
     41 
     42 <img style="margin: 0px auto 10px; display: block; text-align: center;" src="images/viewstub4.png" alt="" id="BLOGGER_PHOTO_ID_5314040638747834450" border="0">
     43 
     44 <p>To use a <code>ViewStub</code>, all you need is to specify an 
     45 <code>android:id</code> attribute, to later inflate the stub, and an 
     46 <code>android:layout</code> attribute, to reference what layout file 
     47 to include and inflate. A stub lets you use a third attribute, 
     48 <code>android:inflatedId</code>, which can be used to override the 
     49 <code>id</code> of the root of the included file. Finally, the layout 
     50 parameters specified on the stub will be applied to the roof of the 
     51 included layout. Here is an example:</p>
     52 
     53 <pre class="prettyprint">&lt;ViewStub
     54   android:id="&#64;+id/stub_import"
     55   android:inflatedId="&#64;+id/panel_import"
     56 
     57   android:layout="&#64;layout/progress_overlay"
     58 
     59   android:layout_width="fill_parent"
     60   android:layout_height="wrap_content"
     61   android:layout_gravity="bottom" /&gt;</pre>
     62 
     63 <p>When you are ready to inflate the stub, simply invoke the 
     64 {@link android.view.ViewStub#inflate()} method. You can also simply change the
     65 visibility of the stub to {@link android.view.View#VISIBLE} or 
     66 {@link android.view.View#INVISIBLE} and the stub will inflate. Note however that the
     67 <code>inflate()</code> method has the benefit of returning the root
     68 <code>View</code> of the inflate layout:</p>
     69 
     70 <pre class="prettyprint">((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
     71 // or
     72 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();</pre>
     73 
     74 <p>It is very important to remember that after the stub is inflated, the stub is
     75 <em>removed</em> from the view hierarchy. As such, it is unnecessary to keep a
     76 long-lived reference, for instance in an class instance field, to a
     77 <code>ViewStub</code>.</p>
     78 
     79 <p>A <code>ViewStub</code> is a great compromise between ease of programming and
     80 efficiency. Instead of inflating views manually and adding them at runtime to
     81 your view hierarchy, simply use a <code>ViewStub</code>. It's cheap and easy.
     82 The only drawback of <code>ViewStub</code> is that it currently does
     83 <em>not</em> support the <a href="layout-tricks-merge.html">&lt;merge /&gt;
     84 tag</a>.</p>
     85