Flattening The Stack

merge2In Dan Morrill’s Future-Proofing Your Apps post yesterday on the Android Developers blog, he wrote:

Due to changes in the View rendering infrastructure, unreasonably deep (more than 10 or so) or broad (more than 30 total) View hierarchies in layouts are now likely to cause crashes. This was always a risk for excessively complex layouts, but you can think of Android 1.5 as being better than 1.1 at exposing this problem. Most developers won’t need to worry about this, but if your app has very complicated layouts, you’ll need to put it on a diet. You can simplify your layouts using the more advanced layout classes like FrameLayout and TableLayout.

If, as you upgrade your app to Android 1.5, you find yourself getting StackOverflowExceptions, that suggests that perhaps your view hierarchy is a bit too deep. Even if your UI is not that complicated in your eyes, you can get to double-digit depth with surprising ease. For example, in 1.1r1, the Dialtacts activity (dialer, contacts, call log, favorites) runs at least a 12-deep view hierarchy.

The issue stems from the fact that Android widgets are intelligent objects, and laying out a UI involves calling a bunch of methods on those objects. In the case of ViewGroup subclasses, such as most layouts, these in turn call a bunch of methods on their child views. Eventually, you run out of stack space, and Android runs with minimal stack on the main UI threads.

If you are looking for ways to trim the fat, here are some suggestions:

1. Use RelativeLayout over nested LinearLayouts. Personally, I love the simple box model that LinearLayout offers, akin to the ones used in Flex and XUL. However, given Android’s implementation, nested LinearLayouts are more expensive, particularly in terms of stack depth. A well-crafted RelativeLayout can give you the same look with less footprint, as Romain Guy explained in February 2009.

2. Use <merge>. Sometimes, you wind up putting another layer in your view hierarchy just to satisfy XML’s need for a single root element in its files. Android offers the <merge> element to use as a placeholder in this case, so you can perhaps skip an extraneous LinearLayout or FrameLayout. Romain Guy covered this topic in March 2009.

3. Eliminate or replace tabs. Tabs add about three levels to your view hierarchy just for being there: one for the LinearLayout to place your TabHost and TabWidget, one for the TabHost/TabWidget itself, and one for the tabs (TextView and ImageView). That may be more than your application can handle, if you have complex tab contents. Consider splitting your tabs out into separate activities, or using some other means to allow people to move between “screens” of content (e.g., ViewSwitcher).

4. Simplify the UI. If you look at many of the built-in applications, you will find that their GUIs are relatively sparse: few buttons or fields except on dedicated data entry forms. Avoiding overly-deep view hierarchies is one of the design criteria that perhaps led to those applications’ UI layouts. While it may be neat to offer a bunch of buttons or whatnot to go along with your list of things to manipulate, the layout tree necessary to put all that stuff on-screen may push you past your stack budget, particularly if you are also using tabs. Consider moving some buttons to option or context menus, or pushing data entry into dialogs or separate activities, to reduce visual clutter and the associated view-hierarchy depth at the same time.

About author

AndroidGuys 4629 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Spotify rolls out new ‘Browse’ page for moods, moments, and more

Spotify announced on Monday that a new “Browse” page has been added to the music streaming service. Designed to further help listeners discover new music and artists as well as

News and Rumors

Droid 2 Launching August 23rd With Froyo Pre-Loaded?

Keep in mind that for the moment, this is *ONLY* a rumor. Android and Me is claiming that a “trustworthy source” has revealed the launch date for the rumored Motorola

News and Rumors

Samsung Announces Official Galaxy S Series Accessories

Samsung has announced the availability of their official premium accessories for the Galaxy S smart phone portfolio of handsets, including the Vibrant and Captivate. These accessories include a desktop dock, vehicle dock and backup battery charging system, and multiple protective case options.


  1. arnaud
    March 29, 14:36 Reply

    Some part of code are missing ?? !!!
    Exemple : attrs file !

  2. Mansi rao
    July 25, 13:39 Reply

    is it not possible to somehow increase the capacity of stack to be capable of holding more layers??

Leave a Reply