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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:15:00+00:00 2026-06-05T02:15:00+00:00

I want to create a custom View , such that when it is inflated

  • 0

I want to create a custom View, such that when it is inflated with wrap_content as one of the dimension parameters and match_parent as the other, it will have a constant aspect ratio, filling whichever dimension is set to match_parent, but providing the layout inflater with the other dimension to be “wrapped”. I presume this is possible because, for example, a full screen width TextView would obviously be able to demand that it have space for two, three or any arbitrary number of lines of text (depending on width), but would not necessarily know this until inflation-time.

Ideally what I want to do is override layout methods in the View subclass such that when the view is inflated, I get the layout information, and supply my own dimensions for the “content” to be wrapped (ie my fixed-ratio rectangle).

I will need to create a lot of these custom views and put them in various different types of layout—sometimes using an Adapter—so really I want to have the maximum control over their inflation I can. What’s the best technique for doing this?

  • 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-05T02:15:01+00:00Added an answer on June 5, 2026 at 2:15 am

    I’ve now solved this with the following code. It’s worth mentioning in passing that the class I’m overriding is a custom ViewGroup with custom children, all using the inherited onMeasure. The children are created and added at construction-time, and I would assume as a matter of course that this is necessary.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float width = MeasureSpec.getSize(widthMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    
        float height = MeasureSpec.getSize(heightMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    
        float nominalHeight = getResources().getInteger(R.integer.nominalheight);
        float nominalWidth = getResources().getInteger(R.integer.nominalwidth);
    
        float aspectRatio = nominalWidth / nominalHeight;
    
        if( widthMode == MeasureSpec.UNSPECIFIED ) { //conform width to height
            width = height * aspectRatio;
        }
        else if (heightMode == MeasureSpec.UNSPECIFIED ) { //conform height to width
            height = width / aspectRatio;
        }
        else if( width / height > aspectRatio //too wide
                && ( widthMode == MeasureSpec.AT_MOST )
                ) { 
            width -= (width - height * aspectRatio);
        }
        else if( width / height < aspectRatio //too tall
                && ( heightMode == MeasureSpec.AT_MOST )
                ) { 
            height -= (height - width / aspectRatio);
        }
    
        int newWidthMeasure = MeasureSpec.makeMeasureSpec((int)width, MeasureSpec.AT_MOST);
        int newHeightMeasure = MeasureSpec.makeMeasureSpec((int)height, MeasureSpec.AT_MOST);
        measureChildren(newWidthMeasure, newHeightMeasure);
    
        setMeasuredDimension((int)width, (int)height);
    }
    

    I’m defining the aspect ratio in terms of a nominal rectangle in resources, but obviously there are plenty of other ways to do this.

    With thanks to Josephus Villarey who pointed me at onMeasure(...) in the first place.

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

Sidebar

Related Questions

I want to create a custom set that will automatically convert objects into a
I want to create a custom style for an activity that will look like
I want to create a custom control (partial view, template) and have it function
I want to create a custom alert view within my iOS application. For example,
I have created a Dynamic View Panel custom control and want to add a
I want to create a custom property on one of my entities mapped from
I want to create tabbed ActionBar (NAVIGATION_MODE_TABS) with custom view (DISPLAY_SHOW_CUSTOM). It should looks
HI have a custom view class that is loaded and placed within my main
I want to create a custom View which contains two buttons without using xml.
I have a question on how to create custom View Helpers. I would like,

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.