{"id":1860,"date":"2016-05-21T06:59:20","date_gmt":"2016-05-21T06:59:20","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1860"},"modified":"2019-06-12T12:00:46","modified_gmt":"2019-06-12T12:00:46","slug":"viewstub","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/viewstub","title":{"rendered":"ViewStub Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, ViewStub is zero sized invisible View that can be used to lazily inflate layout resource at runtime. It is a dumb and lightweight view.\u00a0The special thing about ViewStub is that it neither\u00a0participate nor draw anything in the layout. It has zero dimension.<\/p>\n<p>By term inflate we mean\u00a0<span style=\"line-height: 1.6471;\">load the layout resource at runtime and then ViewStub replace\u00a0itself\u00a0in its parent by the inflated layout resource.<\/span><\/p>\n<p>Layout referenced by a ViewStub is inflated and added to the UI\u00a0only when we\u00a0decide. It means Whenever a ViewStub is made visible, or when inflate() method\u00a0invoked, the layout resource is inflated and then ViewStub replaces itself in its parent with the inflated Resource. The ViewStub only exists in the view hierarchy until\u00a0setVisibility(int)\u00a0or\u00a0inflate()\u00a0is invoked. The inflated View is added to the ViewStub&#8217;s parent using layout parameter.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Sometimes our layout might require complex views that are rarely used. Whether they are item details, undo messages, or progress indicators. You can reduce memory usage and speed up rendering by loading the views only when they are needed.<\/p>\n<hr \/>\n<h4><strong>Basic\u00a0ViewStub XML Code:<\/strong><\/h4>\n<pre>&lt;ViewStub android:id=\"@+id\/simpleViewStub\"\r\nandroid:inflatedId=\"@+id\/subView\"\r\nandroid:layout=\"@layout\/mySubView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"200dp\" \/&gt;<\/pre>\n<p>The ViewStub thus defined can be found using the id &#8220;simpleViewStub&#8221;. After inflation of the layout resource &#8220;mySubView,&#8221; the ViewStub is removed from its parent. The View created by inflating the layout resource &#8220;mySubView&#8221; can be found using the id &#8220;subView,&#8221; specified by the inflatedId property. The inflated View is finally assigned a width of 200dip and a height of 200dip.<\/p>\n<hr \/>\n<h4><strong>Difference between ViewStub and include tag:<\/strong><\/h4>\n<p>The include tag will just include the xml content\u2019s in your xml file. It\u2019s a nice way to share layout parts between different layout\u2019s. The ViewStub tag is a little bit different because it is not directly included, and will be loaded only when you actually need it, i.e when you set ViewStub\u2019s visibility to \u201ctrue\u201d.<\/p>\n<p>This is a nice optimization because you could have a complex layout with tons of small views, complex views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it\u2019ll be loaded.<\/p>\n<hr \/>\n<h4><strong>Important Methods Of ViewStub:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of ViewStub that may be called in order to manage the ViewStub.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. getInflatedId():<\/strong><\/span> This method is used to get the id taken by the inflated view. This method returns an integer type value.<\/p>\n<p>Below we then get the same id using getInfaltedId() method.<\/p>\n<pre>ViewStub simpleViewStub = new ViewStub(getApplicationContext()); \/\/ get the reference of ViewStub\r\nint infaltedId = simpleViewStub.getInflatedId(); \/\/ get the infalted layout's id\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. setLayoutResource(int layoutResource):<\/strong> <\/span>This method is used to set the layout resource to inflate when this StubbedView becomes visible or invisible or when\u00a0inflate()\u00a0is invoked.<\/p>\n<p>Below we set the layout resource that is used while inflating.<\/p>\n<pre>ViewStub simpleViewStub = new ViewStub(getApplicationContext()); \/\/ get the reference of ViewStub\r\nsimpleViewStub.setLayoutResource(R.layout.stub_layout); \/\/ set layout resource to inflate\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. getLayoutResource():<\/strong><\/span> This method is used to get the layout resource that will be used bysetVisibility(int)\u00a0or\u00a0inflate()\u00a0to replace this StubbedView in its parent by another view. This method returns an integer type value.<\/p>\n<p>Below we firstly set the layout resource and then get the layout resource that will be used by\u00a0setVisibility(int)\u00a0or\u00a0inflate()\u00a0to replace this StubbedView in its parent by another view.<\/p>\n<pre>ViewStub simpleViewStub = new ViewStub(getApplicationContext()); \/\/ get the reference of ViewStub\r\nsimpleViewStub.getLayoutResource(R.layout.stub_layout); \/\/ set layout resource to inflate\r\nint layoutReosource = simpleViewStub.getLayoutResource(); \/\/ get layout resource that inflates\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. inflate():<\/strong><\/span> This method is used to Inflates the layout resource identified by getLayoutResource()\u00a0and replaces this StubbedView in its parent by the inflated layout resource.<\/p>\n<p>Below we inflate the layout resource that replaces this StubbedView in its parent by the\u00a0 inflated layout resource.<\/p>\n<pre>ViewStub simpleViewStub = (ViewStub) findViewById(R.id.simpleViewStub); \/\/ get the reference of ViewStub\r\nView inflated = simpleViewStub.inflate(); \/\/ inflate the layout resource\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5.\u00a0setVisibility(int visibility):<\/strong><\/span> This method is used to visibility of the StubView. When visibility is set toVISIBLE\u00a0or\u00a0INVISIBLE,\u00a0inflate()\u00a0is invoked and this StubbedView is replaced in its parent by the inflated layout resource.<\/p>\n<p>Below we set the INVISIBLE visibility of ViewStub.<\/p>\n<pre>ViewStub simpleViewStub = new ViewStub(getApplicationContext()); \/\/ get the reference of ViewStub\r\nsimpleViewStub.setVisibility(View.INVISIBLE); \/\/ set invisible visibility of ViewStub\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. setOnInflateListener(OnInflateListenerinflateListener):<\/strong><\/span> This method is used to listener event when we inflate the ViewStub. It specifies the inflate listener to be notified after this ViewStub successfully inflated its layout resource.<\/p>\n<p>Below we show the use of setOnInflateListener event.<\/p>\n<pre>ViewStub simpleViewStub = new ViewStub(getApplicationContext()); \/\/ get the reference of ViewStub\r\n\/\/ perform setOnInflateListener event\r\nsimpleViewStub.setOnInflateListener(new ViewStub.OnInflateListener() {\r\n@Override\r\npublic void onInflate(ViewStub stub, View inflated) {\r\n\/\/ do something here.\r\n}\r\n});\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of ViewStub:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a ViewStub that helps us to configure it in our layout (xml).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong> <\/span>This attribute is used to uniquely identify a ViewStub.<\/p>\n<p>Below we set the \u201csimpleViewStub\u201d id of the ViewStub that is used to uniquely identify it.<\/p>\n<pre>&lt;ViewStub android:id=\"@+id\/simpleViewStub\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"200dip\" \/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. inflatedId:<\/strong><\/span> This attribute is used to set the id of the inflated View.\u00a0We can also set the id of inflated view programmatically means in java class by using setInflatedId() method.<\/p>\n<p>Below we set the \u201cmySubView\u2019 id of the inflated view.<\/p>\n<pre>&lt;ViewStub android:id=\"@+id\/simpleViewStub\"\r\nandroid:inflatedId=\"@+id\/subView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"200dip\" \/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. layout:<\/strong><\/span> This attribute is used to supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so.\u00a0We can also set layout resource programmatically means in java class by using setLayoutResource(int) method.<\/p>\n<p>Below we set the layout resource that is used while inflating.<\/p>\n<pre>&lt;ViewStub android:id=\"@+id\/simpleViewStub\"\r\nandroid:inflatedId=\"@+id\/subView\"\r\nandroid:layout=\"@layout\/mySubView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"200dp\" \/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>ViewStub Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of ViewStub in which we display a ViewStub and two buttons. Firstly we get the reference of Button\u2019s and ViewStub and then inflate the layout resource. In this we perform click events on Show and Hide Button\u2019s. Show Button is used to show the inflated view and hide button is used to hide the inflated view from the screen. In inflated view i.e custom_stub.xml we display a ImageView with a TextView.<\/p>\n<p>Below you can download code, see final output and step by step explanation of example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ViewStubExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ?<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1917\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ViewStub-Example-In-Android-Studio.gif\" alt=\"ViewStub Example In Android Studio\" width=\"306\" height=\"417\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it ViewStubExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:\u00a0<\/strong><\/span>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we open xml file and then add two Button&#8217;s and a ViewStub. We also add custom_stub layout in the ViewStub.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"fill_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/showButton\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:layout_marginTop=\"50dp\"\r\nandroid:background=\"#0f0\"\r\nandroid:text=\"Show\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"18sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/hideButton\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:layout_marginTop=\"10dp\"\r\nandroid:background=\"#f00\"\r\nandroid:text=\"Hide\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"18sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;ViewStub\r\nandroid:id=\"@+id\/simpleViewStub\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginTop=\"100dp\"\r\nandroid:inflatedId=\"@+id\/inflatedview\"\r\nandroid:layout=\"@layout\/custom_stub\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Create a new xml file i.e custom_stub.xml inside layout and add the below code:<\/p>\n<p>In this file we create a ImageView with a TextView i.e used while inflating the layout. Make sure to add a image name image1 in drawable folder.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;ImageView\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"200dp\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:src=\"@drawable\/image1\" \/&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:text=\"CM PUNK\"\r\nandroid:textColor=\"#000\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span>\u00a0Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step Firstly we get the reference of Button\u2019s and ViewStub and then inflate the layout resource. In this we perform click events on Show and Hide Button\u2019s. Show Button is used to show the inflated view and hide button is used to hide the inflated view from the screen. In inflated view i.e custom_stub.xml we display a ImageView with a TextView.<\/p>\n<pre>package example.abhiandroid.galleryexample;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.view.ViewStub;\r\nimport android.widget.Button;\r\n\r\npublic class MainActivity extends Activity {\r\nViewStub simpleViewStub;\r\nButton showButton, hideButton;\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\nsimpleViewStub = ((ViewStub) findViewById(R.id.simpleViewStub)); \/\/ get the reference of ViewStub\r\nsimpleViewStub.inflate(); \/\/ inflate the layout\r\nshowButton = (Button) findViewById(R.id.showButton); \/\/ get the reference of show button\r\nhideButton = (Button) findViewById(R.id.hideButton); \/\/ get the reference of hide buttton\r\n\/\/ perform click event on show button\r\nshowButton.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ set VISIBLE visibility of ViewStub\r\nsimpleViewStub.setVisibility(View.VISIBLE);\r\n}\r\n});\r\n\/\/ perform click event on hide button\r\nhideButton.setOnClickListener(new View.OnClickListener() {\r\n@Override\r\npublic void onClick(View v) {\r\n\/\/ set GONE visibility of ViewStub\r\nsimpleViewStub.setVisibility(View.GONE);\r\n}\r\n});\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ViewStub is zero sized invisible View that can be used to lazily inflate layout resource at runtime. It is a dumb and lightweight view.\u00a0The special thing about ViewStub is that it neither\u00a0participate nor draw anything in the layout. It has zero dimension. By term inflate we mean\u00a0load the layout resource at runtime and &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/viewstub\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ViewStub Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1860","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ViewStub Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ViewStub with example in Android Studio. In Android, ViewStub is zero sized invisible View that can be used to lazily inflate layout resource at runtime.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/viewstub\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1860"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1860\/revisions"}],"predecessor-version":[{"id":2811,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1860\/revisions\/2811"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}