Android - Is a ViewStub worth it?
Asked Answered
V

7

18

I have a ListView where each row of the listview contains about 10 ImageButtons. Most of these buttons have visibility = Gone and only show up in very rare scenarios. I am wondering if it's worth it to replace these ImageButtons with ViewStubs to avoid loading them (and the images they contain) all the time for all the rows of the listview. Then again their visibility is set to "Gone", so I am not sure what impact loading them has. Do their images actually get loaded or not?

Note that I am talking about replacing e.g. the 8 ImageButtons with 8 ViewStubs, not with 1

Cheers

Ventre answered 29/12, 2015 at 13:32 Comment(2)
I don't know the answer but Im wondering why you even do it this way. Wouldn't it be more intuitive to include an ViewContainer, lets say a LinearLayout and add the needed Images into it with you Adapter? This way you have no unneccesary views attached to you layout.Nations
The idea was just to keep all view declarations in xml and only change the visibility of elements as required in the adapter. So you are basically suggesting instead of changing their visibility in the adapter to add them/remove them in the adapter? When we reuse a view in the getView() via the convertView would you suggest to just remove everything from that view and start fresh or to check for what's there and modify it accordingly?Ventre
P
7

Edit: just noticed that Endzeit commented regarding a similar direction before me.

I would start by doing some benchmarking around the inflating code with and without the views - just comment out the adapter code so it doesn't try to access the non existing views.

If the removal of the Views from the layout does gives you an improvement that you think is necessary and since you say the views are present only in rare scenarios which you are anyway checking for in your adapter, then instead of inflating those views or even using view stubs, create them in code and add/remove them as needed (using the viewholder to reference them).

You could even go further and do a lazy creation of these views, similar to lazy loading of images, but I would only do that after running some benchmarking again.

I would use ViewStubs for loading complex layouts not simple ImageButtons.

Edit 2:

Looking into ViewStub inflate command, which is what it does when it needs to be visible you can see it infaltes the layout given and then adds it to the parent layout - since you are adding a simple ImageButton you can gain performance by not having a ViewStub and just adding the ImageButton in your code. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewStub.java#ViewStub.inflate%28%29

Pointing answered 31/12, 2015 at 18:50 Comment(5)
Your last sentence was my main concern for making this question! I 'll run a few comparisons with TraceView and see where that takes me. CheersVentre
Added some explanation regarding why adding the ImageButton in runtime would be better then using a ViewStub.Pointing
can you have a look at my question in the comments at the top?Ventre
Yes, the idea is to add/remove the views in the adapter - that would be the efficient way in my opinion, especially since you are saying most of the times the views aren't there.Pointing
You can easily check if the holder has a reference to an existing ImageButton and then if needed remove or add it and update its content.Pointing
I
12

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.

Sometimes your layout might require complex views that are rarely used. Whether they are item details, progress indicators, or undo messages, you can reduce memory usage and speed up rendering by loading the views only when they are needed.

Simply a ViewStub is used to increase efficiency of rendering layout. By using ViewStub, manually views can be created but not added to view hierarchy. At the runtime, can be easily inflated, while ViewStub is inflated, the content of the viewstub will be replaced the defined layout in the viewstub.

The ViewStub will be loaded only when you actually use it/need it, i.e., when you set its visibility to VISIBLE (actually visible) or INVISIBLE (still not visible, but its size isn't 0 any more). ViewStub 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.

You must add ViewStub in Layout at first, after you can inflate it to another View.

Note: One drawback of ViewStub is that it doesn’t currently support the <merge/> tag in the layouts to be inflated. Alos ViewStub can’t be used more than once. Also keeping long-lived reference to a ViewStub is unnecessary, if it is required, it's good practice to null it after inflating, so GC can eat it.

Let's suppose your ViewStub ID is view_stub. You need to do the following in the activity:

ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
View inflatedView = viewStub.inflate();
ImageButton button = (ImageButton) inflatedView.findViewById(R.id.button);

Now you can do whatever you want with the button :) That is, the inflate method returns the stub layout which contains the actual elements from the XML file.

Of course, you can always have the onClick XML attribute or can be dynamically called.

Is a ViewStub worth it?
->For the scenarios that you are specifying, I think `ViewStub` will be worth-shot.  

See below urls about ViewStub

http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-with.html

http://developer.android.com/reference/android/view/ViewStub.html

http://developer.android.com/training/improving-layouts/loading-ondemand.html

Instead of ViewStub you can try <\include> tag. 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.

Difference between <include> and <ViewStub> in android

Iranian answered 31/12, 2015 at 18:55 Comment(0)
P
7

Edit: just noticed that Endzeit commented regarding a similar direction before me.

I would start by doing some benchmarking around the inflating code with and without the views - just comment out the adapter code so it doesn't try to access the non existing views.

If the removal of the Views from the layout does gives you an improvement that you think is necessary and since you say the views are present only in rare scenarios which you are anyway checking for in your adapter, then instead of inflating those views or even using view stubs, create them in code and add/remove them as needed (using the viewholder to reference them).

You could even go further and do a lazy creation of these views, similar to lazy loading of images, but I would only do that after running some benchmarking again.

I would use ViewStubs for loading complex layouts not simple ImageButtons.

Edit 2:

Looking into ViewStub inflate command, which is what it does when it needs to be visible you can see it infaltes the layout given and then adds it to the parent layout - since you are adding a simple ImageButton you can gain performance by not having a ViewStub and just adding the ImageButton in your code. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewStub.java#ViewStub.inflate%28%29

Pointing answered 31/12, 2015 at 18:50 Comment(5)
Your last sentence was my main concern for making this question! I 'll run a few comparisons with TraceView and see where that takes me. CheersVentre
Added some explanation regarding why adding the ImageButton in runtime would be better then using a ViewStub.Pointing
can you have a look at my question in the comments at the top?Ventre
Yes, the idea is to add/remove the views in the adapter - that would be the efficient way in my opinion, especially since you are saying most of the times the views aren't there.Pointing
You can easily check if the holder has a reference to an existing ImageButton and then if needed remove or add it and update its content.Pointing
H
4

According to Google's official documentation here.

ViewStub is a lightweight view with no dimension that doesn’t draw anything or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. Each ViewStub simply needs to include the android:layout attribute to specify the layout to inflate.

To experiment this, I created a sample project and added a ViewStub to the layout hierarchy. On running layout inspector I can see that all the layout attributes for ViewStub are zero.

enter image description here

Let's compare it to having a layout which has 10 buttons hidden. What this actually means is, the layout hierarchy has 10 buttons hidden, which is sitting in the layout hierarchy and taking up some amount of memory. It's cheap to leave a ViewStub in hierarchy since it doesn't take up much memory, at the same time it's cheap to inflate.

My final verdict would be, use ViewStub extensively when you've complicated views which are inflated rarely as it definitely helps in saving memory and improving View inflating time.

Hanaper answered 12/10, 2017 at 17:12 Comment(0)
R
2

Using the Android monitor's Memory tab in Android Studio (button for the Android monitor should be at the bottom bar), you could check it yourself:

  • Take a look at the memory usage when running the app with invisible buttons
  • Take a look at the memory usage when running the app with visible buttons

If there is any difference, then you can conclude not everything is preloaded when the views are Gone. Of course, you could also compare this to a ViewStub implementation to check whether that will help to decrease memory usage.

Ranger answered 6/1, 2016 at 9:6 Comment(0)
D
2

In short, using custom view instead of viewstub.

We are having a similar situation now, and we have also tried viewstub before, listview works a little faster. But when it comes to 8 viewstubs, i don't think its a good idea to use viewstub to avoid inflate too many widgets.

Since u (and also us) have a logic to control whether 10 buttons to show or not , why not just define a custom view, and draw different buttons according to different state machine? It's much fast, and need no inflation at all, and it's logic is much better controlled. We are using this method to accelerate listview now and it works good.

Dhow answered 7/1, 2016 at 10:59 Comment(4)
Are you talking about a viewgroup ?Ventre
@sakis In one of our cases, we have to show 6 buttons, each according to one state. we defined an MyView which extends View class, and in it's onDraw method draw the button(it's just a rect with rounded corner) and others like textview. Make MyView clickable and response to touch event(get cilck xy and see if it's inside the rect). If we need any Click Effect,just add animation in changing rect's property.Dhow
Yeah at first that's what I thought, then I started thinking about click listeners and handling all that :P But it does make perfect sense to do that if you have performance issues, that is definitely going to be the most performant. Thanks for the clarification!Ventre
I'm glad it helped ^_^Dhow
B
1

when you set a view visibility to gone this means that This view is invisible, and it doesn't take any space for layout but its data are loaded into it.

Now the ListViews they remove the unseen or lets say the views that are out of the screen bounds for performance reasons .

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

So i think if you want from my opinion I prefer the Views with GONE Visibility rather than using much logic with ViewStub and Creating and inflating ... etc .

But on the other hand

The rendering performance comes into picture when you are inflating the views.

My guess is that its much cheaper to inflate a ViewStub than to inflate a View, either from XML or by changing visibility. ViewStub is especially used when you need to add/remove (indefinite) views (eg. add phone numbers to a given contact). Hope this is what you were looking for.

reference : ViewStub vs. View.GONE

some good Brief presentation of DDMS here : http://magicmicky.github.io/android_development/benchmark-using-traceview/

Base answered 31/12, 2015 at 18:21 Comment(0)
R
-1

Use ViewStub instead ImageButton.

This is because

. ViewStub is zero sized view by default while image button not

. View Stub is naturally an invisible view . its performance is better than image button because it load runtime only when its state become visible.

Rank answered 29/12, 2015 at 14:1 Comment(2)
I edited my question a bit. My ImageButtons have a visibility = Gone, so I am not sure if their images are actually loaded or not?Ventre
This case is far separate than question. if you are setting visibility of view visibility=gone than its your responsibility to put it back to visibility=visible and thats same for both view stub and image button.Rank

© 2022 - 2024 — McMap. All rights reserved.