I have a gray area in understanding the use of Constraint Layout
.
I have implemented my layout with Constraint Layout
. But It happens that I want to make a group of the view items to act as one view, for example making two ImageView
s and a TextView
to respond to click like a single view so I need to put them in another layout and use this layout to respond to click.
I am wondering is it alright to put them in another Constraint Layout
( other layouts can not give the arrangement of views that I want to have) which will led to having nested Constraint Layout
in a single layout.
So generally is it alright to have nested Constraint Layout
or it will be a wrong practice regarding the purpose of it (i.e. having flat layout)?
Edit
As in answers there are suggestions for setting listener for each of these views instead of setting it for their parent layout, I have already tested this approach and it is not what I need. An example is when I have a customized button with text and a drawable (which I can't make it with drawableLeft
or drawableRight
attributes of a button itself because it won't look like what we had in design). So as you see I can't make ImageView
and TextView
listen to the click individually because user will see two different click effect and it won't look like a single button click.
“Using nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice.
ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.
Android Studio provides us with the number of layouts and it might be a bit confusing to choose the most suitable one for your job. Well, each layout has its own benefits but when it comes to complex, dynamic and responsive views you should always choose Constraint Layout.
Does the ConstraintLayout have better performance then a nested Layout? Yes, ConstraintLayout is being designed with performance in mind, trying to eliminate as many pass scenarios as possible and by trying to eliminate the need for deeply-nested view hierarchies.
Based on this article from android developers blog. I would like to mention few things. You can read the complete article and decide for yourself but I would like to mention few key points.
1. How android draws Views
When a user brings an Android view into focus, the Android framework directs the view to draw itself. This drawing process comprises 3 phases:
Each phase within the drawing process requires a top-down traversal of the view tree. Therefore, the more views you embed within each other (or nest) into the view hierarchy, the more time and computation power it takes for the device to draw the views. By keeping a flat hierarchy in your Android app layouts, you can create a fast and responsive user interface for your app.
As constraint layout will allow you to create a flat hierarchy that will use less computational power and will be faster. Based on the result of tests performed against traditional layout, you can see the results below.
Based on my understanding of the way layout works and behaviour of constraint layout, your nested constraint layout will be faster than all traditional layout, But due to nesting it will be slower than complete flat hierarchy. Also, to accomplish your goal of allowing user to click, why not set click listeners to both the view that should work out for you.
I wouldn't nest multiple constraint layouts as that would negate the purpose of flatness as you mention, but if it's just the one, I wouldn't worry about memory efficiency and flatness. As a solution though, you could include a transparent view to cover the group that you want to respond to a single click, and have that overlay view absorb the click. All you have to do is constrain it to the edges of the other views and still maintain one parent constraint layout.
EDIT: After your updated question, I think there's no option for you but create that nested layout. Lint has the max number of nested layouts set to 10, so that alone is an indicator of the threshold that nested layouts can be damaging to the overall performance. I wouldn't worry too much about one nesting layout. You can use systrace for your scenario and see that the difference in time is probably insignificant.
Probably you want to create a Group in your constraint layout and set a click listener for it.
Check the accepted answer for this one. Use group in ConstraintLayout to listen for click events on multiple views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With