1

I have a GridLayout in my xml layout with width and height of 420dp. This GridLayout is filled by n*n buttons; I want to create a n*n board (like Sudoku or chess).

If n were constant I could do this by xml, and set height and width of buttons to 0dp to auto-size them. For example if n is 4, the width and height of each button is 420/4 = 105.

But the problem is n is not constant and is given at runtime by the user, so I can't use xml and must add buttons in Java. Below is the code I use to create them. The buttons are added just fine, but the problem is their size; they are not auto-sized.

I tried to give a size of 0dp in code with btns[i].setWidth(0) or btnsParam.width = 0, but neither worked. It doesn't matter if n is 3 or 10, the button size is always fixed.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.game_screen);

    boardSize = (int)Math.pow(Integer.parseInt(getIntent().getExtras().getString("boardSize")),2);
    btns = new Button[boardSize];

    final GridLayout board = (GridLayout)findViewById(R.id.ninnboard);
    board.setRowCount((int)Math.sqrt(boardSize));
    board.setColumnCount((int)Math.sqrt(boardSize));

    for (int i = 0; i < btns.length; i++) {
        btns[i] = new Button(this);
        btns[i].setTag(i);
        btns[i].setText(0+"");
        btns[i].setBackground(getResources().getDrawable(R.drawable.tiles));
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        GridLayout.LayoutParams btnsParam = new GridLayout.LayoutParams();
        btnsParam.setMargins(px,px,px,px);


        btns[i].setLayoutParams(btnsParam);


        board.addView(btns[i]);


        btns[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnMove(v);
            }
        });

    }

i conform that if n is fixed, i can do this by xml like below(n is 4). but n is not fixed or constant. and is given by user in runtime. so i need to add them by programming.


{<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".Game3Activity">

    <android.support.v7.widget.GridLayout
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimaryDark"
        android:padding="3dp"
        app:columnCount="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.62"
        app:rowCount="4">

        <Button
            android:id="@+id/btn0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="0"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="0"
            app:layout_columnWeight="1"
            app:layout_row="0"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="1"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="1"
            app:layout_columnWeight="1"
            app:layout_row="0"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="2"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="2"
            app:layout_columnWeight="1"
            app:layout_row="0"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="3"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="3"
            app:layout_columnWeight="1"
            app:layout_row="0"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="4"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="0"
            app:layout_columnWeight="1"
            app:layout_row="1"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="5"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="1"
            app:layout_columnWeight="1"
            app:layout_row="1"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="6"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="2"
            app:layout_columnWeight="1"
            app:layout_row="1"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn7"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="7"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="3"
            app:layout_columnWeight="1"
            app:layout_row="1"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="8"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="0"
            app:layout_columnWeight="1"
            app:layout_row="2"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="9"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="1"
            app:layout_columnWeight="1"
            app:layout_row="2"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn10"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="10"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="2"
            app:layout_columnWeight="1"
            app:layout_row="2"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn11"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="11"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="3"
            app:layout_columnWeight="1"
            app:layout_row="2"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn12"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="12"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="0"
            app:layout_columnWeight="1"
            app:layout_row="3"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn13"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="13"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="1"
            app:layout_columnWeight="1"
            app:layout_row="3"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn14"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="14"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="2"
            app:layout_columnWeight="1"
            app:layout_row="3"
            app:layout_rowWeight="1" />

        <Button
            android:id="@+id/btn15"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:background="@drawable/tiles"
            android:fontFamily="@font/artifika"
            android:onClick="btnMove"
            android:tag="15"
            android:text="0"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_column="3"
            app:layout_columnWeight="1"
            app:layout_row="3"
            app:layout_rowWeight="1" />


    </android.support.v7.widget.GridLayout>

    <TextView
        android:id="@+id/hintTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/artifika"
        android:onClick="getHint"
        android:text="@string/hint_name"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/resetTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/artifika"
        android:onClick="reset"
        android:text="@string/reset_name"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/winInformingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="44dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:fontFamily="@font/artifika"
        android:text="@string/win_informing_text"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
        android:textSize="25sp"
        android:textStyle="bold"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/moveInformingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:fontFamily="@font/artifika"
        android:text="@string/move_text"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:textStyle="bold"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/winInformingTextView"
        app:layout_constraintVertical_bias="0.75" />

    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="48dp"
        android:fontFamily="@font/artifika"
        android:text="00:00:00"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/hintTextView"
        app:layout_constraintHorizontal_bias="0.461"
        app:layout_constraintStart_toEndOf="@+id/resetTextView"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>}
  • Possible duplicate of [GridLayout (not GridView) how to stretch all children evenly](https://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly) – TheWanderer Sep 05 '18 at 21:56
  • my mean about autosize is to stretch all children evenly – Hassan Zaree Sep 06 '18 at 12:51
  • That's what that link answers. – TheWanderer Sep 06 '18 at 12:55
  • no, it is not! in this link they are talking about xml. i can't use xml. i must do this by programming. because the n is not constant. if it is i can do by xml and by using girdLayout even not LinearLayout that mentioned on that link. – Hassan Zaree Sep 06 '18 at 18:41
  • Just define your button layout in XML, with the `grid:layout_columnWeight="1" grid:layout_gravity="fill_horizontal"` attributes, and then inflate that XML and add it to your GridLayout. – TheWanderer Sep 06 '18 at 18:44
  • i can't define my buttons in xml. because we don't know how many buttons they are. if n is 3, buttons are 9. if n is 4 buttons 16, if n is 10, buttons are 100. i can define fix number buttons in xml just as i do this before. number of buttons is not fix. – Hassan Zaree Sep 06 '18 at 18:51
  • See how you're calling `new Button()` in your code? You can replace that with `(Button) LayoutInflater.from(this).inflate(R.layout.your_base_button, false, null)`. – TheWanderer Sep 06 '18 at 18:53
  • You could also use `inflate(R.layout.your_base_button, board, true)` and remove `board.addView()`. – TheWanderer Sep 06 '18 at 18:59
  • i did what you told me friend and create my base button as below. i set width and height 0dp for auto sizing. but doesn't work. – Hassan Zaree Sep 06 '18 at 19:38
  • You obviously didn't do what I told you. You don't have either of the properties I said to include. Also, the width and height should be wrap_content. – TheWanderer Sep 06 '18 at 19:39
  • the only difference is buttons comes from xml and just add to girdlayout by programming, not creating in programming. – Hassan Zaree Sep 06 '18 at 19:41
  • i add wrap_content now. no difference with 0dp. – Hassan Zaree Sep 06 '18 at 19:43
  • look at my xml code in question. when n is 4. they are 0 dp. and it's work. when i set them wrap_content that time, it doesn't give me what i want. – Hassan Zaree Sep 06 '18 at 19:45
  • my mean about auto size is to size buttons how that all buttons fill all of girdview. no larger than girdlayout and no smaller. fill it completely. at this time my buttons has same size and they have squad shape. – Hassan Zaree Sep 06 '18 at 19:48
  • i add grid:layout_columnWeight="1" grid:layout_gravity="fill_horizontal" too. not working. – Hassan Zaree Sep 10 '18 at 12:56
  • can't anybody help me? please!! – Hassan Zaree Sep 11 '18 at 14:56

0 Answers0