xtheosirian
xtheosirian

Reputation: 298

Setting height equal to width with width being attributted by weight

I'm making a 2048 based game for android, one of the features I've added is the possibility to have a grid ranging from 4x4 to 8x8, but I'm having problems while distributing the size of the tiles. I have a LinearLayout containing other LinearLayouts, one for each row (grid height), and inside each row TextViews for each tile in that row. Each textview has weight 1 and the row containing them has weightSum equal to width (in tiles) and width set to match_parent, and then I set the height to be equal to the width after adding the textviews to the layout to have a square. I'm doing this via code, inflating a predefined textview for each tile and adding it to the row, then adding the row to the "board" view. But it isn't working.

This is the relevant piece of code that attributes the width, height and weight for the views in the onCreate method:

        board = (LinearLayout) findViewById(R.id.board);
        board.setWeightSum(1.0f * height);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int i = 0; i < height; i++) {

            LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row_ll, null);
            lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
            row.setId(1000 + i);
            row.setWeightSum(1.0f * width);

            for (int j = 0; j < width; j++) {

                tv = (TextView) inflater.inflate(R.layout.text_view, null);

                tv.setId(nextId());

                row.addView(tv, lp);

            }

            board.addView(row);

            for (int j = 0; j < width; j++) {

                tv = (TextView) findViewById((i * width) + j + 1);
                tv.setHeight(tv.getMeasuredWidth());
                System.out.println("h:" + tv.getHeight() + "; w:" + tv.getWidth());

            }

        }

text_view:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:layout_width="0dip"
android:layout_height="0dip"
android:background="@drawable/bg_tv"
android:gravity="center"
android:text="@string/zero"
android:textColor="@color/text"
android:textSize="16sp"
android:layout_weight="1" >

</TextView>

row_ll

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_ll"
android:layout_width="match_parent"
android:layout_height="64dip"
android:background="@drawable/bg_tv"
android:orientation="horizontal"
android:weightSum="4">

</LinearLayout>

The rest of the code can be found at https://github.com/lhamaware/2048

Upvotes: 2

Views: 2025

Answers (1)

chiuki
chiuki

Reputation: 14700

You need to set height during the measure phase. To do that, make a custom view extending TextView, and override onMeasure:

public void onMeasure(int widthSpec, int heightSpec) {
  super.onMeasure(widthSpec, heightSpec);
  int width = getMeasuredWidth();
  setMeasuredDimension(width, width);
}

See my blog post for more info: http://blog.sqisland.com/2012/01/android-square-view.html

Upvotes: 5

Related Questions