gravity="fill_vertical" vs. layout_height="fill_parent" in Android - android

Why does gravity offer a "fill_vertical" option? How is this any different from setting layout_height "fill_parent"? What if I choose a fixed layout_height and "fill_vertical" for gravity? Won't this contradict?

I believe that using gravity="fill_vertical" indicates that the View should take up as much space as possible vertically while still respecting other Views' height constraints. If you use layout_height="fill_parent", you're telling the View to take up the entire parent's height; this may result in a single View pushing other Views off the viewport.

Related

ConstraintLayout View with 0dp height has a different actual height when using math_parent and 0dp as width

This problem is better described with an example:
As you can see in the resulting rendered layout, the heights from first_view and second_view are different, and the only thing that is different is that the first_view uses
layout_width=match_parent
instead of
layout_width="0dp";
layout_constraintStart_toStartOf="parent";
layout_constraintEnd_toEndOf="parent".
Is it a bug or the expected behavior?
Taken from the official docs:
Important: MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
and official training
Note: You cannot use match_parent for any view in a ConstraintLayout.
Instead use "match constraints" (0dp).
That being said, your example is also not valid because you are using 0dp (match constraints) for height without specifying the bottom constraint which might lead to unexpected behaviour of the view. To match constraints for a dimension you need to declare both ends.

Force LinearLayout child to use all available space but at least enough to fit its content

I'm struggling with getting LinearLayout to behave like I want it. Unfortunately, both dimension modes MATCH_PARENT and WRAP_CONTENT don't seem to fit for my purposes.
Here's why: I want the child that is added to the LinearLayout to be completely visible. Nothing should be cut off. So normally, I should use WRAP_CONTENT to achieve this behaviour.
But, if there's more space in the LinearLayout than the child really needs, I also want it to fill that space. This is of course what MATCH_PARENT is for.
However, I can't use MATCH_PARENT because in case there is less space in the LinearLayout than my child needs, using MATCH_PARENT will cut off the child which I don't want.
So this leaves me somewhat puzzled as to how I can achieve what I want: How can I allow a child to fill additional space in the LinearLayout (if available) while at the same time forcing the LinearLayout to be at least as big as the child needs in order to be completely visible?
Put your child view inside a ScrollView with width MATCH_PARENT, height MATCH_PARENTand set both the child's dimensions MATCH_PARENT
Instead of using MATCH_PARENT for your child, use WRAP_CONTENT and set your child's weight to 1,So it will take all the empty space in your LinearLayout. Suppose your LinearLayout's height is 128dp, and your child 's height is 56dp, your child will be 128dp . If you set WRAP_CONTENT on your LinearLayout, it will be 56dp, and it child will still take all the place it needs.
If you want your LinearLayout to have a minimum width or height, and not match_parent, you can do setMinimumWidth() or setMinimumHeight() in your xml layout or in your code programmatycally depending your childs default height and width.

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.

What is an Android view's measured state?

I'm having a hard time finding example usage, or explanations, of the Android View concept of "measured state".
To be clear, this is different from measured width and measured height. See in Android docs.
The general idea is that a View has a desired width and height, which is constrained by two factors.
The first constraint is the parent View to which the View is attached. This will describe the maximum width and size of the View. If the View has asked to be wider or taller than the parent view, then it's measured height and measured width will be constrained such that the values are no greater than the parent's. These values, which can be MATCH_PARENT, WRAP_CONTENT or a dp value, get converted to measured widths and heights once the parent View's width and height constraints are included. This guarantees that no child is larger than it's parent.
The second constraint comes from the View's siblings, or in other words, from other Views attached to the same layout. The ViewGroup (Layout) will resolve a View's width and height such that it displays correctly. These resolved width's and height's are the values you get from getWidth() and getHeight().
There is a lot of good documentation on the Android development portal. In particular look at the Layout section of the View class: http://developer.android.com/reference/android/view/View.html#Layout
Edit: Sorry for misunderstanding your question. I've taken a look at the concept of measured state for a view, and I can only find a single documented state: MEASURED_STATE_TOO_SMALL. This leads me to believe that it's use may be very limited and it's existence is primarily for the purpose of future functionality or to be made use of by custom Views/ViewGroups.
The documentation for MEASURED_STATE_TOO_SMALL states the following:
Bit of getMeasuredWidthAndState() and getMeasuredWidthAndState() that
indicates the measured size is smaller that the space the view would
like to have.
This leads me to believe that the bit will be set whenever a fixed dp/px value is given to the View which is larger than the parent's width and height and therefore the View's measured width and height will have been scaled down.
If you manage to find more states or additional information as to where it is used, please to update the question. All the best!

Set layout_width (height) to 0dp inside RelativeLayout

I know the effect of setting layout_width (or height) to 0dp in combination with layout_weight in a LinearLayout, as answered in this question:
Why is 0dp considered a performance enhancement?
However, in the example code on the Android developer guide here
http://developer.android.com/guide/topics/ui/layout/relative.html
Why do they set the layout_width of the Spinner to 0dp ? So what is exactly the reason or the trick behind this ? My guess for this is the horizontal position for the Spinner can already be determined by the relative attributes, so they just specify it as 0dp for the same reason as above case. However, I can't seem to find actual documentation on this so it's still a bit confusing.
When a child view is anchored on both sides (in RelativeLayout), the width is ignored. In this case, the Spinner knows it width because of
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times"
I've seen this trick in several projects too, and the reason is exactly the same why they do it in LinearLayout with "weight", but here the key attribute is: android:layout_toLeftOf="#+id/times" it does exactly the same trick as "weight" would do with linear layouts, so this is a way to say, take all the space remaining from android:layout_alignParentLeft="true" until android:layout_toLeftOf="#+id/times" without hardcoding any specific width...
Regards!
we use 0dp while using with layout_weight because it will neglect that attribute and it will go as per the layout_weight..

Categories

Resources