Propagate change of View dimensions to children / parents - android

In my Android app I want to change the height of a RelativeLayout in an animation. The layout contains a hierarchy of children, which are all the same size as the parent.
If I change the height of the parent view by adjusting its LayoutParams, the children keep their sizes. Changing the inner child's size also doesn't change the parent's size, and changing the size of all of the View at the same time also doesn't seem to work.
How do I increase the height of several nested views at the same time?

Related

Android: Layout height in percentage of screen resolution inside scroll view

I have a scrollView with a contstraintLayout inside , i need to set the same height value in percent of screen independently from the scrollView and his content height
for example:
if it's possible not programmatically but via XML properties.
Already tried layout_constraintHeight_percent, but it follow the parent height and is not what i need

Custom View onMeasure vs onSizeChanged vs onLayout

I have implemented some custom views in Android and one of the most frequent problems I have is to adapt the layout to the screen, weather by matching the parent's content, MATCH_PARENT, or wrapping its own content, WRAP_CONTENT. The ConstraintLayout, for instance, does not support WRAP_CONTENT.
The trouble starts when I have to adapt the children size, which initially I was doing on onLayout. One use case of this is when I want to have children which width and height respect a certain aspect ratio; and the width grow as much as possible.
So, in my onLayout, if changed was true I had reset constraints reset. However, it was causing a loop, since it changes the layout again.
What would be the proper place to size the custom widget children?
onSizeChanged or onMeasure?

How to control what happens when a view's wrap_content exceeds its parent's width/height?

In Android, given any View - I want its height/width to both be wrap_content - but I don't want this value to ever exceed its parent. How do I achieve this in XML?
The width/height of a View can never exceed its parent. Just use wrap_content.

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

How does match_parent work?

As far as I've understood Android's View rendering process, the root Layout will start the measurement and ask each of its children for it's size. If one of them is a Viewgroup itself, it will again ask the children, which either just return their set size, or calculate it if they're set to wrap_content.
Now how does that work with match_parent? The view can't calculate it's size since it has to know the parent's size first, right? So the parent would have to calculate it's own size and then send that to the child, which then can go on with it's own layout.
Does that make match_parent a little more inefficient than wrap_content or even fixed dp-values?
Actually, match_parent says that "Hi parent, I want to be as big as your left available space".For example, if ViewGroup Parent has two children: child A at index 0 with wrap_content and child B at index 1 with match_parent. And A says he wants to be 100px wide, then the width of B will be : total width of Parent - padding of Parent - margin of A - 100px - margin of B

Categories

Resources