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.
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.