I'm having a hard time understanding what is wrong with my onMeasure. I expect a 2x2 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 2x2 grid would be greatly appreciated. While the example is a 2x2 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.