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

  • SEARCH
  • Home
  • 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 6239363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:22:51+00:00 2026-05-24T11:22:51+00:00

Answered I have a RelativeLayout where I am adding views dynamically as the user

  • 0

Answered

I have a RelativeLayout where I am adding views dynamically as the user scrolls vertically or horizontally. I have rolled my own ViewRecycler since there is potentially thousands of views that could compose the whole of what can be scrolled, but I only show 30 or so at any time. Think a zoomed in view of a calendar.

I am running into performance problems when I add the views that are about to be seen, onMeasure is called on the RelativeLayout cascading down to onMeasure getting called on all of it’s child views. I already have the calculated size of how big the RelativeLayout will ever be and have set that on it’s LayoutParameters, so measuring the ViewGroup isn’t necessary, nor is re-measuring the Views that have already been added with their final size and the newly added view has no bearing on those view.

The simple example to demonstrate the problem is adding/removing a View to a RelativeLayout and watching the onMeasure get called despite the fact that it doesn’t affect the RelativeLayout’s size or the position of other Views.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shell"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</LinearLayout>

MyActivity.java

public class MyActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewGroup shell = (ViewGroup) findViewById(R.id.shell);

        final RelativeLayout container = new RelativeLayout(this) {
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                Log.d("MyActvity", "onMeasure called on map");
            }
        };
        container.setBackgroundColor(Color.rgb(255, 0, 0));
        ViewGroup.LayoutParams containerParams = new ViewGroup.LayoutParams(300, 300);

        final TextView childView = new TextView(this);
        childView.setBackgroundColor(Color.rgb(0, 255, 0));
        childView.setText("Child View");

        Button viewToggle = (Button) findViewById(R.id.button);
        viewToggle.setText("Add/Remove Child View");

        viewToggle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                if (childView.getParent() == null) {
                    container.addView(childView, 400, 30);
                } else {
                   container.removeView(childView);
                }
            }
        });

        shell.addView(container, containerParams);
    }
}

Running this, you would see 2 initial (an expected) calls to onMeasure, then one for each time that you add/remove the view by clicking the button. This obviously runs fine, but you can see where constant calls to onMeasure when you have a complex layout of nested views can get problematic.

Is there a recommended way to bypass these onMeasure calls or at least onMeasure calling measureChildren?

  • 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-05-24T11:22:51+00:00Added an answer on May 24, 2026 at 11:22 am

    Instead of rolling my own Layout Manager (which I may still do in the future), I changed the onMeasure to:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; count > i; i++) {
            View v = getChildAt(i);
    
            if (v.getVisibility() != GONE) {
                if (v.getMeasuredWidth() <= 0 || v.getMeasuredHeight() <= 0) {
                    measureChild(v,
                        MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,
                            MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(v.getLayoutParams().height,
                            MeasureSpec.AT_MOST));
                }
            }
        }
    
        setMeasuredDimension(resolveSize(staticContainerWidth, widthMeasureSpec),
            resolveSize(staticContainerHeight, heightMeasureSpec));
    }
    

    … and added a sudo-hard coded height and width for the container as a variable. Setting these to what you expect is out of the scope of this solution.

    int staticContainerHeight = 300;
    int staticContainerWidth = 300;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basics have already been answered here . But is there a pre-built PHP
Edit: This was user error (sorry). I have answered it below. I am developing
There have been several questions that have answered the HOW or more precisely how
EDIT ~ I have answered my own question below in the EDIT section, not
First off, I'm aware this is a bad practice and I have answered many
I know I have already answered a similar question ( Running Batch File in
Question: I have a question that is apparently not answered by this already-asked Bash
I have javascript string variable with var sttr=We prefer questions that can be answered
Great site! I have a question I haven't seen answered. I am very new
User kokos answered the wonderful Hidden Features of C# question by mentioning the using

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.