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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:57:09+00:00 2026-06-04T07:57:09+00:00

I’ve been trying to find a solution to this, but perhaps I’m not understanding

  • 0

I’ve been trying to find a solution to this, but perhaps I’m not understanding how the “onMeasure()” method works so well in Android!

This is the first time I’ve made custom views “dynamic” in Android, so I learned that you need to override the “onMeasure()” method in order to resize your view depending upon how large the screen is. I would like the my view to be half the width of the screen, plus a little extra (this number is arbitrary, as long as its relatively small, between 3 and 5), so I wrote this code for the onMeasure method:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int halfWidth = (MeasureSpec.getSize(widthMeasureSpec) / 2) - 5;

    this.setMeasuredDimension((int) halfWidth, (int) halfWidth);
}

I’m sure this a terrible way of going about things because everywhere I’ve found online has so much more code than I do while trying to accomplish something similar!

When I place these views next to each other in the XML, this first view is correctly sized (it is indeed half the size of the width minus a little more), but the second view is much smaller, probably half the size of the width of the last view minus a little more! (I would post a picture of it but unfortunately I don’t have enough reputation to do so…)

What should occur is the two boxes should be the same size, no matter where in the XML I place them. They should all be one-half the size of the width of the screen, minus a little extra.

I know it has something to do with how I’m using “setMeasuredDimension()”, but I don’t know what it is. I really hope someone could clarify things a bit for me! Thanks! 🙂

  • 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-04T07:57:10+00:00Added an answer on June 4, 2026 at 7:57 am

    I actually accomplished this today through a little bit of experimentation based upon what Tim said. I moved away from “onMeasure()” and only wrote this code for it:

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // Call the super class.
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        // Set the dimensions of this view.
        this.setMeasuredDimension((int) width, (int) width);
    }
    

    Width is a global float. Where does width come from? I derived width from this method that I wrote in the view to resize:

    public void initSize(float size)
    {
        width = size;
        this.invalidate();
    }
    

    I call “invalidate()” to redraw the view. Something has to call this method though, so I do so in the activity. First, I get the size of the screen in the Activity, not the View, using this code:

    Display thisDisplay = this.getWindowManager().getDefaultDisplay();
    Point desiredSize = new Point();
    display.getSize(desiredSize);
    int width = size.x;
    

    You used to be able to call a method called “getWidth()” on “thisDisplay” but this method is now depreciated, so you should use “getSize()” instead. Finally, right after you initialize the constructor, call the “initSize()” method:

    nameBox = (InfoBoxView) findViewById(R.id.nameBox);
        nameBox.initSize(width);
    

    Now, all the views will be the same size. I don’t know if this is the best way to do this, but it works for now and I figured I’d post it here so everyone knew. 🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.