Is constraint layout more flexible in locating views in Android Studio? - android

Both constraint and relative view groups support relative positioning of a view wrt other.
Relative layout provides 4 different attributes : layout_toRightOf / toLeftOf / toTopOf / toBottomOf ,
and Constraint provides many combinations of format " layout_constraintTop_toTopOf "
BUT can’t we place the views at any position just using the 4 attributes of Relative layout ?
In what way is Constraint layout more responsive ?

According to Google Developers ConstraintLayout has better performance than RelativeLayout. In addition (as you've mentioned) it's more advanced than RelativeLayout and allows you to build your Layout more precisely with all these additional constraints options. What's more, if you set the view A's width or height to match_constraint (0dp), and set its end constraint to start of view B, the A's width or height can be dynamically scaled when B's visibility is gone.
Any view inside ConstraintLayout requires only two constraints for X and Y axis e.g. layout_constraintTop_toTopOf and layout_constraintStart_toStartOf.

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.

How to indicate width is 'flexible' in Android layout editor?

I want to keep distance from left and right, but keep width of the widget flexible. How can I define it in Android?
I do not understand your question, but i guess you're looking for a way to give your ListView a width which is dynamic based on e.g. the screen width.
You should have a look at MATCH_PARENT
LayoutParams are used by views to tell their parents how they want to
be laid out. See ViewGroup Layout Attributes for a list of all child
view attributes that this class supports.
The base LayoutParams class just describes how big the view wants to
be for both width and height. For each dimension, it can specify one
of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to
enclose its content (plus padding)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For
example, AbsoluteLayout has its own subclass of LayoutParams which
adds an X and Y value.
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
https://developer.android.com/guide/topics/ui/declaring-layout.html

How to do match_parent in iOS

I am used to having an Android background where I do:
android_layout_width="match_parent" or android_layout_height="match_parent"
How is this behavior done on iOS, using xib?
1) Set frame of child view matching parent's view bounds.
2) Enable autoresizing mask like in screenshot.
You have to create two constraints from the child to it's parent view:
equal width
equal height
It's also possible to define a margin (select the created constraint within the size inspector)

Android - How can I determine an appropriate height for my ListView items?

I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.

How To Scale A Layout While Keep Its Children Original Scale

Can I Scale A Layout(e.g LinearLayout) from 0.5 to 1.0 while Keeping the size And position of the TextView inside the layout to 1.0 During the whole transformation (the overflow parts should hidden)?
I see That The Scale Animation in IOS Keep the scale of the Children And How Can I Achieve this in Android ?
Plus,On Starting Animation, The Parent Layout of the animated block has already make space for it,But in IOS ,the parent makes room gradually during the animation, does it have an option for me to do the same thing in Android ?
This can be done pretty easily with the Property Animation API.
havent tried it on LinearLayout but if the view children are ordered correctly with their "layout_width" & "layout_height" you can use the ValueAnimator class to scale the parent view via LayoutParams.width & height.
Only setback is API 11 and above.

Categories

Resources