Check Mobile App
Read now
Start a Project

Android ViewStub using Data Binding

Save

ViewStub:

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.

When a ViewStub is made visible, or when inflate() is invoked, then layout resource is inflated. ViewStub replaces itself in its parent with the inflated View or Views. ViewStub exists in the view hierarchy until setVisibility(int) or  inflate() is invoked.

You can reduce memory usage and speed up rendering by loading the views only when they are needed.

Define ViewStub in XML file

 

Inflate ViewStub: 

We can specify layout at run time (viewStub!!.layoutResource) or in XML layout (android: layout)  attribute. A ViewStub can be load by using visible, or inflate().

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Vedesh Kumar (Moderator)
    I can inflate the views inside a LinearLayout and can get the same results. Why use this ViewStub?
    • Friend
      UI performance. There are times when the UI component i.e. activity or fragment has too many UI elements but all the elements might not be visible every time. So if you are concerned about UI performance when your UI component has many view/views-group then one can have ViewStub which is lazily inflated when it’s visibility is enabled.
  • css.php

    When I think in getting solutions for my Ecommerce Platform, I think of Webkul. They have been such a blessing in our business. We have spent thousands of dollars in support and modules from them. We love their support team. They work very closely with us, and they always go the extra mile when we need help.

    Michael Urresta

    Michael Urresta

    Talk to Sales

    Global

    Live Chat
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home