Android – ViewStub example

By -
Problem: How to implement ViewStub in Android?
Description:

I have used ViewStub almost for implementing Title Bar (i.e. Header) for most of the application. I have defined a Title bar layout only for once and then I have re-used the same layout for every activity by the use of ViewStub.

Now, let me clear idea for “What is ViewStub” actually?

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.

I am sure you have also came across this article: Layout Tricks: Using ViewStubs

Output of this example code:

ViewStub demo

Download Full example code here: Android ViewStub example

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...