How to sort LinearLayout's children programatically - android

I have Linear Layout with horizontal orientation which has few ImageViews in it (hardcoded in the xml). I have to sort them based on MultiselectListPreference (all the checked first and unchecked later). To put checked ones in the beginning I tried
((LinearLayout)imageView.getParent()).removeView(imageView);
((LinearLayout)imageView.getParent()).addView(imageView,0);
And did nothing with unchecked. But it gives me the "The specified child already exists. You must call removeView(view) on view's given parent" though I've already called it. This may be because all those ImageViews are present in xml. How can I obtain the desired arrangement of child ImageViews. If at all LinearLayout does not suffice, should I use RecyclerView here , would it be heavier?

Cast the parent view to ViewGroup so the removeView method will work.
((ViewGroup)imageView.getParent()).removeView(imageView);

Related

Displaying view/progress spinner/error view

I have a frame layout with 2 children. A custom view and a progress bar.
Only 1 at a time is visible. Works as I need.
I want to also add some error view which will display an error and the other components would be hidden.
Is using a FrameLayout a good idea or should I switch to RelativeLayout?
If the layout works for you I would stick to FrameLayout. Since there are only 3 children (and assuming that none of them has children) the difference in performance should be really minimal (if any), but one difference I noticed digging into the source code of both FrameLayout and RelativeLayout is in the onMeasure method, FrameLayout will iterate twice all of its children, RelativeLayout iterates from 3 to 7 times (!)you can check FrameLayout onMeasure and RelativeLayout onMeasure yourself

At what point are child views attached to the parent view

I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.

Dynamically add a view in a custom layout after know layout's size

I want to dynamically add a view in a custom layout (extends RelativeLayout). But the subview must be placed with specific coordinate and size. So I initialize and use addView into the method onMeasure (same problem with onLayout) of my custom layout, like this I can know the size of the layout.
But sometimes (after changing device’s orientation for example) the method onMeasure (or onLayout) are called multiple times (often twice). So the subviews are added many times.
Does onMeasure is the right place to dynamically add subviews ?

How to restrict bounds for Translate Animation for a view in Android?

Let me explain the scenario that I want to achieve:-
Consider the below as the Layout I have inside a Parent_Linearlayout:
[Linear Layout] (Fill_Parent, Wrap_Content)
[ScrollView]
Activity's setContentView is set to the Parent_Linearlayout
In the application, when a condition is met, I want the Scrollview to be removed from the screen and instead put another View in its place.
I've been able to do this, & when I remove the ScrollView, I'm applying translate Animation to it so that it seems as if the View has gone to the top -before removing it.
But when the animation occurs, the ScrollView translates OVER the Linear layout present above it.
How do I restrict it, so that the scrollview does not go over the linear layout, but disappears at the base of the Linearlayout. I want the linearlayout to always stay visible..
I've been trying to do this from quite some time, but I've not been able to get desired results..
Could someone kindly help me out here??
I don't quite understand your description of your layout, but the Android view system is drawn based on the ordering of the views in the hierarchy. Views added later to a parent are drawn after those added earlier. So if you always want the LinearLayout to be drawn on top of the ScrollView if/when they overlap, then declare or add the ScrollView object to its parent before the LinearLayout object.
In thinking more about this, I suppose the ordering here is important because you want the ScrollView to be placed below the LinearLayout in the parent of both of these views. Putting the ScrollView first (and thus having it painted first) would then put it above the other LinearLayout, which isn't what you want.
There are various ways to achieve what you want. For example, you could use a RelativeLayout as the parent of the views, then the ordering is not important.
Alternatively, you could place the ScrollView inside another LinearLayout (and that LinearLayout would be the second child of the overall parent layout). Then when you animate the ScrollView, it would be clipped by its immediate parent, which I believe would give you the effect you're looking for (make sure that setClipChildren() is set to true on this new intermediate LinearLayout, which it is by default, otherwise it won't clip the ScrollView as it animates out of it). Note that this approach would necessitate different animation values, since you are now animating the view outside of its parent (the new LinearLayout).

Add view to XML layout programmatically and make its z order below an existing view

I have an XML layout with some custom tabs, a heading, and a ProgressBar(main.xml). I wish to add another XML layout(home.xml) to the main.xml layout, as i wish to keep main.xml re-usable for other activity's layouts and simply add things to it as necessary.
home.xml contains a ScrollView and a TextView. I am currently using my Activity's LayoutInflator to add home.xml to main.xml:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.home, rootLayout);
rootLayout is the root layout of main.xml and is a RelativeLayout
The problem: after inflating R.layout.home into rootLayout, it seems as though the ProgressBar contained in rootLayout is hidden underneath the content of home.xml
Is there a way to tell certain views(via XML) to float above other views when the layout is constructed in this way?
if not, am i forced to use methods such as progressBar.bringToFront() to raise targeted views to the top?
what alternatives do i have in z-ordering views when some layouts are constructed using inflation?
edit: it seems as though the bringToFront() method is not doing what i expect - i call it on one of my Button views and it still appears to be ordered below all other views(which were inflated) and remains unclickable
You could try using ViewStub instead of manually adding it and set the set the inflateId with setInflateId(R.layout.home)
http://developer.android.com/resources/articles/layout-tricks-stubs.html
Edit:
Google seem have to moved the android developer from their android page to blogspot.
Updated Link:
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html
Views are drawn based on their order in the parent. For instance, the child at index 0 is always (at least with the current layouts, except Gallery) drawn first and the child at index count - 1 is always drawn last. bringToFront() simply moves a View from its current position in the parent to the last. Note that changing the index of a child can affect its position on screen (for instance in a horizontal LinearLayout, moving a child from index 0 to index count - 1 will also move it from the far left to the far right.)

Categories

Resources