ViewStub in Android
Most of the time we are using <include> to inflate other XML files in the main XML. but it will always cost us heavy use of CPU and battery.
what if we can have something which will inflate when we request?
let's explore ViewStub…
A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.
It is a dumb and lightweight view and it has zero dimension
It is used to lazily inflate layout resource. The <ViewStub> views are inflated on call.
When the ViewStub is inflated, it replaces itself in its parent view with the inflated layout resource. Its visibility will exist in the view hierarchy only when setVisibility(int) or inflate() is invoked.
The inflated view is added to ViewStub’s parent using the layout parameter.
We can use ViewStub in the following places, for example — progress Indicators, undo messages, some item details etc.
It reduces memory usage and speeds up rendering by loading the views only when they are needed.
Method availability in ViewStub
- getInflatedId() — To get the id taken by the inflated view.
- setLayoutResources(int layoutResource) — Via this method, we can set the layout resource to inflate when StubbedView becomes visible or invisible or when inflate() is invoked.
- getLayoutResource() — To get the layout resource that will be used by setVisibility(int) or inflate() to replace this StubbedView in its present with another view. The resultant will be an integer value.
- inflate() — In order to Inflate the layout resource that gets identified by getLayoutResource() and replace this StubbedView in its parent by the inflated layout resource.
- setVisibility(int visibility) — Sometimes there is a need to make the visibility to invisible and later on visible.
- setOnInflateListener(OnInflateListenerinflateListener) — Via this call, It specifies the inflate listener to be notified after this ViewStub successfully inflated its layout resource.
An attribute in ViewStub
id — To uniquely identify a ViewStub
inflated — To set the ID of the inflated View. we can set it by using the setInflatedId() method.
layout — To supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so.
we can be set by using the setLayoutResource(int) method.
ViewStub vs Android <include>
The < include /> will just include the XML contents in our 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.
The benefit of the <ViewStub> is we can have a complex layout with tons of small views or headers, and still have our activity load up really fast. Once we use one of those views, it’ll be loaded.
The loading of ViewStub is must faster than <include> views. For example, if we have 100+ <include>views takes 80 times more CPU time than 100+<ViewStub>.
The battery power consumption is more with <includes> as compare to <ViewStub>.