I want to set a blank relativeLayout then start adding some views from another relativeLayout.
I want to do this because at the moment I have many bitmaps in a single layout which causes memory errors. So i want to achieve the effect of adding and removing views as I see fit in the code.
At the moment I am using the setContentView() of a layout on the onCreate() which causes me to have memory erros since there are too many views to add at once.
Another way. Perhaps it is possible to setContentView() of the layout with too many views. Only making it not adding any views before I code it to add specific views.
setContentView(R.layout.start_up_screen_denmark);
// This will add all the views in the layout causing a memory error. Making everything below irrelavant.
// So perhaps there is a way to set the ContentView without adding all the views automaticly.
// Perhaps i can set a blank layout and add views to that layout from the other layout at will.
ImageView denmark = (ImageView) findViewById(R.id.GoDenmark);
ViewGroup parent = (ViewGroup) denmark.getParent();
parent.removeView(denmark);
Using a LinearLayout might be better suited for what you're trying to do since you can easily add and remove views from a LinearLayout with lin_lay.addView(View child, int index) and lin_lay.removeViewAt(int index).
However, it also sounds like you might want to look into a RecyclerView if you have a lot of bitmaps in one layout and it's causing memory issues.
Related
I have two layouts, manageLayout and mainLayout. They have constraints to the parent on all sides. I need in a one time have manageLayout on the top of mainLayout, and on the other time mainLayout on the top of manageLayout. Of course, there is sense in using visibility=gone on one of them, but i need one layout on the background of another. Problem: layout on the background handle events from top layout. How to make lower layout(and his elements) untouchable when another layout is risen?
Layout tree image:
LayoutTreeImg
Code sample, where i want to disable communications with lower layout: https://pastebin.com/PeL7u3YD (not only isSaveEnabled=false had no effect, also isEnabled=false had no effects too)
If you just need an explanation.
Once you've initialized both your views for mainLayout and manageLayout, you will need to set an empty onClickListener on both of them. Basically, layouts should get the click but do nothing. This way you can block the layout and widgets underneath the view on Front from getting clicked.
Now for for switching view to front maintain a boolean to know which view is on the front and on your button click set the other view bringToFront() (Or try some other ways mentioned here if you want) and don't forget to switch the boolean value.
Let me know if this works for you or you have any issues regarding this.
According to my perception, you can make lower layout setEnable(false). I hope it will work.
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
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
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
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.
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.
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.
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.
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/
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.
I have a parent Linear Layout inside which there are two Linear Layout and a button.Each Linear Layout inside the parent Linear layout has two items in it. Items include a edit text and a spinner.what I want is to swap this two LinearLayout when i click the button.So my question is How do i do that programatically?. I am new to Android development so please help me to solve this.
On your outermost LinearLayout, try calling removeViewAt(0). This should remove the first LinearLayout. Then call addView() passing it the first LinearLayout.
One simple way would be to just make two layouts. Designed such that when you are ready to use the other one, switch layouts and then set that as the current view.
Or you could do so with various animation classes if trying to get fancy with it. If there is an exact visual effect you are trying to receive, perhaps provide a bit more on what you want.
As Karim mentioned, you can use setVisibilty() to View.GONE and the to View.VISIBLE.
But for a smooth swapping you are going to need to learn about Translate Animation.
Here a working example of how to swap two views (e.g. two LinearLayouts):
ViewGroup root = findViewById(R.id.my_root);
// assumption: root has 2 child views only
// swap left and right (or top and bottom)
View leftView = root.getChildAt(0);
root.removeViewAt(0);
root.addView(leftView);
// now the two child views of root are swapped
It may sound foolish, but I actually can't find anything on it.
Is it ok to add multiple views to the root view of activity in Android?
So for example I could go like this:
setContentView(R.layout.main);
setContentView(gLView);
setContentView(otherView);
Or simply retrieve it as a view
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(view1);
layout.addView(view2);
layout.addView(view3);
layout.addView(view4);
It all seems to work on devices I test, but is it guaranteed to work on all of them?
Or should I artificially create single FrameLayout to add to root view and add everything to this FrameLayout?
More experiments:
1) if I don't use setContentView and print:
Log.d("Test", this.getWindow().getDecorView().findViewById(android.R.id.content).getClass().getName());
I get: android.widget.FrameLayout
2) If I set content view to for example GLSurfaceView and print the same Log.d
It also prints android.widget.FrameLayout
3) And if I dont' use setContentView at all, and simply do
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(myView);
It attaches the view
So I assume that android.R.id.content returns not what you set by setContentView but the parent of what you set, or the actual root view in activity (which turns out is FrameLayout?)
And I am able to add multiple children to it this way, but question is:
Am I allowed to do that?
Yes, it's perfectly fine to add multiple content Views at the root level. The content Views are simply added to a FrameLayout container, and it is best practice to simply use it for your layout if a FrameLayout is all you require for it, instead of adding an additional container layer.
If you are adding a new content View, then you should use the addContentView() method instead of setContentView(), which would cause the existing content to be replaced instead.
Also, it is possible to add multiple Views to the content container in XML layouts as well by using the <merge> tag, which would just replace the base FrameLayout.
Calling setContentView several times will simply replace whatever view you set before. So what you want to do is create a root view that contains the multiple views you want to add and set the root view to your Activity with setContentView. Your second example with the FrameLayout is a good approach.
Your approach is valid at some extent. but its nice practice to call the setcontentView() once in activity.
this is because it will be very easy to maintain the Activity life cycle and reduce the app crash due to layout leak.
I personally call the setcontentView() once. I define all the layout in single XML file. Afterwords I call setVisibility(View.VISIBLE) and setVisibility(View.INVISIBLE) or setVisibility(View.GONE) to show or hide any particular layout containing my certain views.
Edit
Also its very easy to adjust the layout in XML because you can use simple drag and drop and If you are using relativeLayout then it will be very easy to place any view any where. but in Java code its some how difficult to place the views on your desired position
Hope this helps :)
I am trying to figure out what is the proper way to remove an ImageView from a RelativeLayout is? I have tried
relativeLayout.removeView(someImageView)
but this causes some issues on certain phones. See this other post here for stacktrace. Removing ImageView causes crash on certain phones
I really don't want to do the solution where you just make it appear gone because that is basically leaking that ImageView in memory right?
someImageView.setVisibility(View.GONE);
Any other ideas would be much appreciated.
If you remove a view from a relative layout, you must reset the relative layout parameters for all other views which are dependent on the view that you are removing. Otherwise, these other views will reference your removed view and throw a NPE.
Consider using another root layout design (like LinearLayout) if you want to achieve this without using View.GONE