Overlay's height does not match_parent when adding view dynamically - android

I have a RelativeLayout (A), inside which I have 2 children:
- LinearLayout (B) (with some stuff)
- below it I have another RelativeLayout (C)
I wanted to add an overlay on top of it. So all I did was added a View with match_parent as it's width and height, and transparent background. All works great, as long as I do not add views dynamically to the nested RelativeLayout (C).
When I do that my overlay stops at the last element that was not added dynamically to the RelativeLayout (C). So the height of the overlay does not match_parent anymore.
I tried to go around it, and add the overlay after I add those dynamically added views to (C), but that also didn't do the trick.
What does the trick is if I set height explicitaly to the overlay after all views are created by getting getHeight of the main root of the view. That is however not the solution I want, as I want a clean solution, that I explained above.
Currently what I did to bypass it for the moment, is I added an overlay with a humongous height(like 2000dip) and set it's bottom alignment to RelativeLayout(C). That does the trick perfectly (the effect of it), however I still would preffer to know the reason why adding dynamicaly those views does not update the height of the overlay (also if I add it dynamically after previous views).
Just to mention, I tried to do requestLayout(), invalidate(), and postInvalidate() on the main container after all views were added. No change.
ps. Might be important piece of information: this root RelativeLayout stated in the beginning is inside a LinearLayout, which is a single child of a ScrollView

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

Dimming background around view

Given an activity with multiple views, is there a way to have the background dimmed while one view retains its original state (not dimmed)?
I have an existing layout with views and I am adding a dimmed framelayout over the existing layout, and would like to keep one of the child views in the existing views "lightened up" in its state while the dimmed framelayout view encompasses around it.
The way I know how to do this won't be very elegant for your particular use case. But I'll explain it in case you don't find a better solution.
Wrap your entire layout in a RelativeLayout, and then add a LinearLayout with fill_parent parameters on top of it, and a partially transparent black background. Adjust the alpha and RGB to how you see fit.
Now you can add your undimmed view to the RelativeLayout on top of the dimming. Since you're no longer inside the FrameLayout container, you'd have to adjust the positioning of this layout, and manually remove and re-add these two views for dimming and undimming, hence this is not a very elegant solution.

Scroll Artifacts on any scrolling view if not wrapped inside a layout without margins

I have solved a weird issue, and I would like to know why it happens.
My application creates screens with FrameLayout as the root element. For Simplicity, let´s say I have a Framelayout with some scrolling elements inside, for example a ScrollView with a TextView, and a GridView.
These child elements are positioned with LayoutParams so I assign them a Width, a Height, and a Margin Left / Margin Top. The usual case is their sizes do NOT match the parent, that is, they are floating inside the parent FrameLayout.
For non-scrolling elements, everything is cool.
But for scrolling elements, when scrolling, I can see all kinds of leftovers OUTSIDE the child bounds. Inside the bounds the scrolling is perfect, but the elements are not clipped properly.
I did a lot of tests, played with setClipChildren / setClipToBounds, changed the root element from FrameLayout to RelativeLayout, etc etc, without success.
I only succeeded when I wrapped the scrolling elements inside another Framelayout/RelativeLayout without any margins, and then assigned the scrolling elements MATCH_PARENT/MATCH PARENT.
The scroll is perfect now, clipping is properly done, but I have added another level of complexity to the layout.
Why does this happen?

New position of layout during animation is resetting after ui interaction

I am working in android 2.3 and I have an issue which is a bit of a pain.
I have 2 layouts on top of each other in a FrameLayout. The top layout is moved partly off-screen during an animation, and the .layout() method is called in the animation end. This works great and the ui is moved correctly and the buttons of the toplayout is still clickable.
After the animation the second layout is visible as the toplayout is moved off-screen. This layout contains a list which is clickable and each cell has an ImageView which is updated when clicked. However my problem is, when the table cell is clicked and the ImageView is updated, the entire view is reset, ignorering the animation and new location of the toplayout. It just sets the toplayout back on top as the animation never happend.
I use an TranslateAnimation and the set fillafter is set to true as well.
Do you guys have any suggestions to what I might be missing.
I guess your problem is placing objects inside FrameLayout. As described in this answer you cannot place a view inside a FrameLayout by setting its location.
It's true that with FrameLayout all children are pegged to the top
left of the screen, but you still have some control with setting their
padding. If you set different padding values to different children,
they will show up at different places in the FrameLayout.
Use padding instead to "move" the view off-screen.

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).

Categories

Resources