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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:28:04+00:00 2026-06-07T04:28:04+00:00

I am trying to make a custom view that is square, using the width

  • 0

I am trying to make a custom view that is square, using the width as the height. I am also using a pre-defined layout which I inflate as it’s UI. As soon as I overrode onMeasure, the custom view no longer appears. Here is my code:

public class MyView extends RelativeLayout{

    public MyView(Context context) {
        super(context);
        addView(setupLayout(context));
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addView(setupLayout(context));
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        addView(setupLayout(context));
    }

    private View setupLayout(Context context) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View myView = inflater.inflate(R.layout.view_layout, null);
        return myView;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(widthMeasureSpec));
    }

}

I have 2 questions:

  1. How do I override onMeasure so that it draws my view the way I am expecting it to?
  2. Is there any way I can make this more efficient in terms of the view hierarchy (i.e. not be putting a RelativeLayout inside a RelativeLayout)
  • 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-07T04:28:06+00:00Added an answer on June 7, 2026 at 4:28 am

    You can use this code from Jan Němec’s answer to a similar question :

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.LinearLayout;
    
    public class SquareLayout extends LinearLayout {
    
    public SquareLayout(Context context) {
        super(context);
    }
    
    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
         @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
    
            if (width > (int)(mScale * height + 0.5)) {
                width = (int)(mScale * height + 0.5);
            } else {
                height = (int)(width / mScale + 0.5);
            }
    
            super.onMeasure(
                    MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
            );
        }
        }
    

    Or try to use this library project.

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

Sidebar

Related Questions

I am trying to make a custom Calendar view and for that I use
Im trying to make a custom view that will have a dynamic number of
I'm trying to make a custom text view that has the font set from
I'm trying to make a custom segue so that the destination view controller slides
I'm trying to make a custom view in Django admin. I'm reading from this
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I am trying to make Custom RelativeLayout which can scale and scroll. Right now
Trying to make a custom :confirm message for a rails form that returns data
I'm trying to make a custom tabgroup in appcelerator in which one (or two)
I'm trying to make a custom converter that inherits from DependencyObject , but it

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.