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 7853783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:48:02+00:00 2026-06-02T19:48:02+00:00

I’m creating a custom layout class and the views within the layout are sizing

  • 0

I’m creating a custom layout class and the views within the layout are sizing properly.
The problem I’m having is that if I include another layout within that one the size of all the nested view’s children are basically full screen sizes.

XML Layout Code – The contents within the FrameLayout are not sizing properly.

<CustomLayout>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"/>

<FrameLayout android:layout_height="wrap_content" android:layout_width="wrap_content">
  <!-- The views in here are sizing themselves as full screen. -->
  <include layout="@layout/panel_emptypicture" android:id="@+id/EmptyPictureRelativeLayout" android:visibility="visible" />
</FrameLayout>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" />
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" />
</CustomLayout>

Java Code for custom layout.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int numChildren = getChildCount();

    Dimension d = new Dimension(0, 0, this.getWidth(), this.getHeight());
    final float scaleX = this.getWidth() / virtualWidth;
    final float scaleY = this.getHeight() / virtualHeight;

    for(int i = 0; i < numChildren; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final Dimension lpd = lp.getDimension();

        Dimension dest = lpd.scale(scaleX, scaleY, d);
        child.layout(dest.x, dest.y, dest.width + dest.x, dest.height + dest.y);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width, height;
    float maxWidth = MeasureSpec.getSize(widthMeasureSpec);
    float maxHeight = MeasureSpec.getSize(heightMeasureSpec);

    final float widthVirtualMultiplier = virtualWidth / virtualHeight;
    final float heightVirtualMultiplier = virtualHeight / virtualWidth;

        if(maxWidth * heightVirtualMultiplier < maxHeight) {
            width = (int)maxWidth;
            height = (int) (width * heightVirtualMultiplier);
        } else {
            height = (int)maxHeight;
            width = (int)(height * widthVirtualMultiplier);
        }


    setMeasuredDimension(width, height);

    // Tried adding this to see if it would do anything. Nope, no effect.
    final int numChildren = getChildCount();
    for(int i = 0; i < numChildren; i++) {
        final View child = getChildAt(i);
        measureChild(child, widthMeasureSpec, heightMeasureSpec);
    }

}
  • 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-02T19:48:06+00:00Added an answer on June 2, 2026 at 7:48 pm

    This ended up being the code that was stable enough for my needs. It still had the bug but oh well.

    The entire project is hosted here if you need the other referenced classes. http://code.google.com/p/motivatormaker-android/

    package com.futonredemption.makemotivator.widget;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewDebug;
    import android.view.ViewGroup;
    
    import com.futonredemption.makemotivator.util.RectUtils;
    
    public class ScaledCompositeLayout extends ViewGroup {
    
        @ViewDebug.ExportedProperty(category = "virtual-measurement")
        public float virtualHeight;
    
        @ViewDebug.ExportedProperty(category = "virtual-measurement")
        public float virtualWidth;
    
        @ViewDebug.ExportedProperty(category = "clamp-width")
        protected boolean clampOnWidth;
    
    
        public ScaledCompositeLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public ScaledCompositeLayout(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
    
            final TypedArray styleValues = context.obtainStyledAttributes(
                            attrs,
                            com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout,
                            defStyle, 0);
    
            virtualWidth = styleValues.getDimension(
                            com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_VirtualWidth,
                            -1);
            virtualHeight = styleValues.getDimension(
                            com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_VirtualHeight,
                            -1);
            clampOnWidth = styleValues.getBoolean(
                    com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_ClampOnWidth,
                    false);
    
            styleValues.recycle();
    
            if(virtualWidth == -1) {
                throw new IllegalStateException("virtualWidth is required parameter.");
            }
            if(virtualHeight == -1) {
                throw new IllegalStateException("virtualHeight is required parameter.");
            }
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            final int numChildren = getChildCount();
    
            Rect parentRect = new Rect(0, 0, this.getWidth(), this.getHeight());
            final float scaleX = this.getWidth() / virtualWidth;
            final float scaleY = this.getHeight() / virtualHeight;
    
            for(int i = 0; i < numChildren; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                final RectF lpr = lp.getRectF();
    
                Rect dest = RectUtils.scale(lpr, scaleX, scaleY, new RectF(parentRect));
                child.layout(dest.left, dest.top, dest.right, dest.bottom);
    
                //Dimension out = dest;
                //throw new IllegalStateException(String.format("Position: %d x %d -- Size: %d x %d", out.x, out.y, out.width, out.height));
            }
        }
    
        /**
         * Uses the Virtual Width and Height to determine the ratio size of the actual layout.
         * The layout_width and layout_height parameters are used to determine the maximum bounds.
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int width, height;
            //int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            //int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            float maxWidth = MeasureSpec.getSize(widthMeasureSpec);
            float maxHeight = MeasureSpec.getSize(heightMeasureSpec);
    
            if (maxHeight == 0.0F) {
                clampOnWidth = true;
            }
    
            final float widthVirtualMultiplier = virtualWidth / virtualHeight;
            final float heightVirtualMultiplier = virtualHeight / virtualWidth;
    
            //final float widthActualMultiplier = maxWidth / maxHeight;
            //final float heightActualMultiplier = maxHeight / maxWidth;
            /*
            if(widthMode == MeasureSpec.EXACTLY) {
                width = (int)maxWidth;
                height = (int) (maxWidth * heightVirtualMultiplier);
            } else if(heightMode == MeasureSpec.EXACTLY) {
                width = (int)(maxHeight * widthVirtualMultiplier);
                height = (int)maxHeight;
            } else { // Use scaling */
                if(clampOnWidth || maxWidth * heightVirtualMultiplier < maxHeight) {
                    width = (int)maxWidth;
                    height = (int) (width * heightVirtualMultiplier);
                } else {
                    height = (int)maxHeight;
                    width = (int)(height * widthVirtualMultiplier);
                }
            //}
    
            setMeasuredDimension(width, height);
    
            final int numChildren = getChildCount();
            final float scaleX = width / virtualWidth;
            final float scaleY = height / virtualHeight;
            final Rect parentRect = new Rect(0, 0, this.getWidth(), this.getHeight());
    
            for(int i = 0; i < numChildren; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                final RectF lpr = lp.getRectF();
    
                Rect dest = RectUtils.scale(lpr, scaleX, scaleY, new RectF(parentRect));
    
                this.measureChild(child, MeasureSpec.makeMeasureSpec(dest.width(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(dest.height(), MeasureSpec.EXACTLY));
    
                //Dimension out = dest;
                //throw new IllegalStateException(String.format("Position: %d x %d -- Size: %d x %d", out.x, out.y, out.width, out.height));
            }
    
            /*
            for(int i = 0; i < numChildren; i++) {
                final View child = getChildAt(i);
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
            */
    
            //throw new IllegalStateException(String.format("%d x %d -- Physical: %.0f x %.0f -- Virtual: %.0f x %.0f", width, height, maxWidth, maxHeight, virtualWidth, virtualHeight));
        }
    
        @Override
        public LayoutParams generateLayoutParams(AttributeSet attrs) {
            return new ScaledCompositeLayout.LayoutParams(getContext(), attrs);
        }
    
        @Override
        protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
            return p instanceof LayoutParams;
        }
    
        @Override
        protected ViewGroup.LayoutParams generateLayoutParams(
                ViewGroup.LayoutParams p) {
            return new LayoutParams(p);
        }
    
        public static class LayoutParams extends
                android.view.ViewGroup.LayoutParams {
    
            public RectF virtualRect = new RectF();
    
            public RectF getRectF() {
                return virtualRect;
            }
    
            public LayoutParams(Context c, AttributeSet attrs) {
                super(c, attrs);
                float virtualWidth;
                float virtualHeight;
    
                TypedArray styleValues = c
                        .obtainStyledAttributes(
                                attrs,
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout);
                virtualRect.top = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualTop,
                                0);
                virtualRect.left = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualLeft,
                                0);
                virtualRect.right = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualRight,
                                0);
                virtualRect.bottom = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualBottom,
                                0);
    
                virtualWidth = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualWidth,
                                -1);
                if (virtualWidth > 0) {
                    virtualRect.right = virtualRect.left + virtualWidth;
                }
    
                virtualHeight = styleValues
                        .getDimension(
                                com.futonredemption.makemotivator.R.styleable.ScaledCompositeLayout_Layout_VirtualHeight,
                                -1);
                if (virtualHeight > 0) {
                    virtualRect.bottom = virtualRect.top + virtualHeight;
                }
    
                styleValues.recycle();
            }
    
            public LayoutParams(int width, int height) {
                super(width, height);
            }
    
            public LayoutParams(ViewGroup.LayoutParams source) {
                super(source);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build

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.