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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:59:01+00:00 2026-06-12T07:59:01+00:00

I’m having a hard time understanding what is wrong with my onMeasure. I expect

  • 0

I’m having a hard time understanding what is wrong with my onMeasure. I expect a 2×2 grid with the following layout but when I check the width and height, the height is being calculated as expected but the width is Integer.MAX_VALUE. I can’t figure out what is happening to the height that isn’t happening to the width. Any help in helping me figure out the correct onMeasure() method to get the expected 2×2 grid would be greatly appreciated. While the example is a 2×2 grid, I need a solution that works for any size of grid.

Following is the XML. Note that DragLayer simply extends FrameLayout. I don’t believe the custom tags will cause any formatting difference but I left them in just in case.

<?xml version="1.0" encoding="utf-8"?>
<com.hogenson.dragndrop.DragLayer 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.hogenson.dragndrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    custom:sfen="@string/start_sfen" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="r" />

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="p" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="K" />

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="R" />

        </TableRow>

    </TableLayout>

</com.hogenson.dragndrop.DragLayer>

And here is the onMeasure() method I am trying to implement in the DragView class.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY)
    {
        width = Integer.MAX_VALUE;
    }

    if (heightMode != MeasureSpec.EXACTLY)
    {
        height = Integer.MAX_VALUE;
    }

    this.setMeasuredDimension(width, height);
}

Edit:

The solution Eric provided below worked perfectly for me with the addition that I needed to add layout_weight to the horizontal layouts as well, which seems to be bad programming practice.

  • 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-12T07:59:02+00:00Added an answer on June 12, 2026 at 7:59 am

    Nitpick

    Could it be the fact that, in both your if statements, you’re modifying width?

    if (widthMode != MeasureSpec.EXACTLY)
    {
        width = Integer.MAX_VALUE;
    }
    
    if (heightMode != MeasureSpec.EXACTLY)
    {
        width = Integer.MAX_VALUE; // Shouldn't this be height?
    }
    

    That aside…

    Before I get to my preferred solution, I’ll start by suggesting that you attach android:stretchColumns="*" to your TableLayout. This will allow the columns to fill the allotted space (and not be too small).

    Using LinearLayouts instead

    However, I think what you want to accomplish is best done with LinearLayouts. Basically, this:

    <LinearLayout
        ...
        android:orientation="vertical" >
    
        <LinearLayout
            ...
            android:orientation="horizontal" >
    
            <com.hogenson.dragndrop.DragView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <!-- more cells for row 1 -->
    
        </LinearLayout>
    
        <LinearLayout
            ...
            android:orientation="horizontal" >
    
            <com.hogenson.dragndrop.DragView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <!-- more cells for row 2 -->
    
        </LinearLayout>
    
    </LinearLayout>
    

    Lastly, squares

    Finally, if you want your objects to be square (same width and height), have a look at this answer.

    Note that, with the above solutions, you’re probably best not to fiddle with onMeasure in this case; the XML should be able to take care of it for you. And as always, good luck!

    • 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 want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

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.