Constraint Layout details

from the CommonsWare Community archives

At December 27, 2019, 5:10pm, Pvr asked:

In XML , i have Constraint Layout as Parent.
1 . Any other layout is allowed inside this constraint layout as inner layout (child)
2 . If allowed , Can i give constraints to inner layout


At December 27, 2019, 10:20pm, mmurphy replied:

Yes.

Any immediate child of a ConstraintLayout can have constraints. So, suppose you have a layout like:

<ConstraintLayout>
  <LinearLayout>
    <ImageView>
    <ImageView>
  </LinearLayout>
  <TextView />
</ConstraintLayout>

(I am just showing the elements to keep this short)

The LinearLayout and the TextView can (and should) use constraints to size and position them within their ConstraintLayout parent. However, neither ImageView can have constraints, as they are children of a LinearLayout, not a ConstraintLayout.

However, you are welcome to nest ConstraintLayout containers:

<ConstraintLayout>
  <ConstraintLayout>
    <ImageView>
    <ImageView>
  </ConstraintLayout>
  <TextView />
</ConstraintLayout>

Now the ImageView widgets can use constraints to size/position them within the inner ConstraintLayout.

Normally, we do not need inner containers like this when we use ConstraintLayout. One scenario where we do is when the inner container has a different background, directly or provided by something else (e.g., a CardView).


At December 28, 2019, 4:12pm, Pvr replied:

Okay Thank you .
I also want to how to know whether some action is performed on a view with in a certain period of time ( eg. Whether a Button is clicked in last 1000ms of time )


At December 28, 2019, 4:25pm, mmurphy replied:

Store the start time in a field (e.g., System.currentTimeMillis()). Compare that with the current time when the event occurs.


At December 29, 2019, 5:20am, Pvr replied:

What difference below layout structure can make in performance or in any aspect if i use


At December 29, 2019, 12:18pm, mmurphy replied:

Sorry, but I do not understand your question.


At December 29, 2019, 3:13pm, Pvr replied:

What difference i can see if i use following cases in my XML
case 1 : Constraint Layout as Parent and Frame layout, Linear layout as children
case 2 : Linear Layout as Parent and Frame layout, Linear layout as children


At December 29, 2019, 3:50pm, mmurphy replied:

I guess I still do not understand the question. ConstraintLayout can do many things that LinearLayout cannot, both on its own (e.g., chains, circular constraints) and in conjunction with helper objects (e.g., Guideline, Barrier). And, as I noted earlier, you do not normally need “Frame layout, Linear layout as children” of a ConstraintLayout, unless you need to provide a background behind some collection of children.


At December 29, 2019, 4:28pm, Pvr replied:

Okay
I’m facing problem when i inflate constraint layout with 3 Buttons in Linear way and there is no problem when i inflate constraint layout with Linear layout for those Buttons
Problem is Button text and Button background are not proper as given


At December 29, 2019, 4:49pm, mmurphy replied:

Without the layouts to examine, and without screenshots or something that shows us what exactly “Button text and Button background are not proper as given”, there is very little that we can do to advise you further. A ConstraintLayout, on its own, does not affect the text or background of widgets that are children of the ConstraintLayout.