Difference between <include> and <ViewStub> in android
Asked Answered
L

4

27

What are the differences between <\include> tag and <\ViewStub> tag and which one is preferrable while designing the layout.

Limbic answered 4/7, 2011 at 4:41 Comment(0)
E
38

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, ie, when you set its visibility to VISIBLE (actually visible) or INVISIBLE (still not visible, but its size isn't 0 anymore). 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.

Enlighten answered 4/7, 2011 at 6:9 Comment(4)
You can set a view's visibility to "true"? Aren't VISIBLE, INVISIBILE and GONE the only visibility options for views? Is the view loaded only when the view is VISIBLE, or will it be loaded when it is not GONE?Polymorphism
@RestInPeace Indeed, not sure why I wrote that (it's been 3 years now...). From the documentation : "When visibility is set to VISIBLE or INVISIBLE, inflate() is invoked and this StubbedView is replaced in its parent by the inflated layout resource.". So it is inflated when the visibility is set to anything but GONE.Enlighten
but, I can use include with visibility to gone or invisible, what is the advantage of ViewStub over that ?!!Munitions
@Gregory: My apologies. I was responding to a flag on your post, and didn't realise how old this post was.Benzedrine
T
15
  • include
    It is used to reuse layout resource
  • ViewStub
    It is used to lazily inflate layout resource
Tye answered 6/8, 2013 at 3:56 Comment(0)
U
5

Sharing and reusing layouts is very easy with Android thanks to the tag, sometimes even too easy and you might end up with user interfaces that contain a large number of views, some of which are rarely used. Thankfully, Android offers a very special widget called ViewStub, which brings you all the benefits of the without polluting your user interface with rarely used views.

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.

Uterus answered 29/7, 2012 at 16:7 Comment(1)
android-developers.blogspot.com/2009/03/…Geelong
I
0

Another important difference is related to layout inflating. with it is not possible to change the layout already static inflated in XML, it is necessary to replace the view and set programmatically al the layout parameters. With it is possible to define (for e.g.) height, width, etc... and inflate different layout at runtime time

Instant answered 4/6, 2015 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.