Nested Constraint Layout? is it a bad practice or not?

Akram picture Akram · Dec 17, 2018 · Viewed 8.7k times · Source

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

Answer

karan picture karan · Dec 17, 2018

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:

  1. Measure: measure view and view groups.
  2. Layout: determine position of childview based on measure.
  3. Draw: Create canvas for each object and draw the view.

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.

enter image description here

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.