Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:32:24+00:00 2026-06-11T13:32:24+00:00

I am building a recursive hierarchical layout. Here is my message_with_replies.xml (I’ve removed the

  • 0

I am building a recursive hierarchical layout. Here is my message_with_replies.xml (I’ve removed the alignment attributes and some items to make the idea clear):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/toggleReplies"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_expand_replies" />
    <LinearLayout
        android:id="@+id/replies"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</RelativeLayout>

In Java code, I inflate the items with this layout and embed them into the replies layout of the parent, and then repeat this with the children, so they form a tree:

root item
- child 0 of root
- - child 0 of child 0 of root
- - child 1 of child 0 of root
- - - child 0 of child 1 of child 0 of root
- child 1 of root
- child 2 of root
- child 3 of root
- - child 0 of child 3 of root

Everything looks and performs fine when I’m on the top level or about 2-3 levels from the top, but as I go deeper, the layout responds slower and slower. The slowdown is non-linear, and on the depth of about 7 or 8 the UI completely stucks (the emulator raises an exception after thinking for about half a minute).

Obviously, the problem is embedded layouts. I embed a linear layout into the relative one, so each new level of my hierarchy means two additional embeddings in terms of UI layouts.

How to optimize this?

I could try to get rid of the linear layout, but I expect that will not solve the problem but will only postpone it to some deeper level because I will still have the embedding.

Is it generally possible to keep the hierarchy flat when I inflate a sub-item with ahother layout? Can I inflate the item and then take its contents without their container?

Added: I’ve bypassed the problem. Now I build my tree using margins, so the layouts are not embedded, and the UI doesn’t stuck. Everything looks and works fine.

However the original question is not yet answered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:32:24+00:00Added an answer on June 11, 2026 at 1:32 pm

    Everything looks and performs fine when I’m on the top level or about
    2-3 levels from the top, but as I go deeper, the layout responds
    slower and slower. The slowdown is non-linear, and on the depth of
    about 7 or 8 the UI completely stucks (the emulator raises an
    exception after thinking for about half a minute).

    As you already seen the problem is the dept of your layout file. Each time you inflate the message_with_replies.xml and add it to the LinearLayout you increase the dept of your layout by 2(which will reduce the performance). So, by the time you get to the 7-8 level you would have already exceeded the recommended maximum layout dept(10, if I’m not mistaken) by quite a lot.

    How to optimize this?

    If you want to continue to inflate the layout in an old view then there isn’t much to do. Otherwise you would add the views to a master layout without the extra ViewGroups between.

    Is it generally possible to keep the hierarchy flat when I inflate a
    sub-item with ahother layout? Can I inflate the item and then take its
    contents without their container?

    You’ll keep the hierarchy flat if you only add Views to the layout. As long as you add ViewGroups to ViewGroups(like you do in your code) you’ll increase the hierarchy’s dept. An option to avoid adding unnecessary ViewGroups(in some cases) is to use the merge tag(you can find more details here).

    Added: I’ve bypassed the problem. Now I build my tree using margins,
    so the layouts are not embedded, and the UI doesn’t stuck. Everything
    looks and works fine.

    This should have been your first option instead of inflating the layout again and again as it is a much simpler solution. The layout could be obtained by using a simple LinearLayout(or a RelativeLayout, I don’t know your setup as you removed the layout attributes from the layout) with orientation set to vertical. Into this layout you would add the desired views, taking in consideration to add the margin based on the level you want this child to be. If you want to simplify things even more you would create a layout file like below(this works if the Button from the layout is set to be on top of the LinearLayout):

    <merge>
        <Button android:id="@+id/toggleReplies">
        <!-- content of the replies at this level -->
    </merge>
    

    After you inflate this layout you would need to set the padding(x padding for the first level, 2x for the second level, 3x for the third level etc) and then add it to the LinearLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a search with some custom objects and three scopes: All , Active ,
I have tried to get my head around building a recursive function to handle
I'm building a project with a recursive Makefile. The basic format is simple: source
I'm building a tree (bill of materials style), and transforming some data. Consider the
Building an inventory system. I have lots of products and each product has three
Building a new Mobile Web Platform for Mobile Users to purchase & download content
building a site using PHP and MySQL that needs to store a lot of
Building a Django app on a VPS. I am not very experienced with setting
Building a website that has English & Japanese speaking users, with the Japanese users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.