4

What is the maximum number of LinearLayouts that can be nested ? Is it infinite, or is there a limit that Android-studio emphasizes ? Or is that device dependent?

OBX
  • 5,834
  • 7
  • 31
  • 70

2 Answers2

9

View tree depth is in practice limited by the UI thread stack size that is needed for the recursive traversal of the view tree in measure/draw operations. The stack size depends on the API level and is 8kB, 12kB or 16kB. There isn't a specific number as the depth limit; in practice you'll see StackOverflowErrors in low-spec devices after a couple dozen nested views or so.

Lint will nag if you have nesting level 10 or deeper in a single layout file. It doesn't analyze the runtime layout hierarchy depth.

Consider keeping your view hierarchies as flat as possible.

Community
  • 1
  • 1
laalto
  • 144,748
  • 64
  • 275
  • 293
  • Assume I am creating Linear layouts inside another from Java Code, will the app crash when the stack overflows ? – OBX May 18 '15 at 13:16
  • Yes. The view hierarchy is in memory regardless of whether you create it in your code or inflate from a layout XML. – laalto May 18 '15 at 13:18
  • That was a valuable information! Other than developer.google , any other source to get quality and deep info in Android Development ? :) – OBX May 18 '15 at 13:21
  • do you have any idea about that how many maximum views can we have in a android layout xml? is there any limit or not – Faisal Khan Dec 08 '15 at 11:27
3

Deep layouts - Layouts with too much nesting are bad for performance.

Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance.

The default maximum depth is 10.

Have a read for more information.

Don Chakkappan
  • 7,157
  • 5
  • 43
  • 57