2

The title explains what I need. I want to set columnWeight of Button (Because is inside GridLayout) but from code.

Something like that with code:

<Button android:layout_column="0"
    android:layout_row="0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1" />
jalopaba
  • 8,039
  • 2
  • 44
  • 57
Oriol Lopez
  • 369
  • 2
  • 6
  • 19

3 Answers3

1

You need to use LinearLayout subviews. Like the user Price says:

There are limitations when using the GridLayout, the following quote is taken from the documentation.

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group."

Here is a small example that uses LinearLayout subviews. (I used Space Views that takes up unused area and pushes the buttons into desired position.)

<GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="1"
    >
        <TextView
            android:text="2x2 button grid"
            android:textSize="32dip"
            android:layout_gravity="center_horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:text="Button 2" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 3" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:text="Button 4" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </GridLayout>

Check Price's response for more information.

Community
  • 1
  • 1
0

Try it: ((GridLayout.LayoutParams) button.getLayoutParams()).columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • did you add button first ?, pls show me your code, where you add this button – mdtuyen Nov 29 '15 at 17:11
  • I don't know you want add button to gridlayout programmatically or the button have been added to gridlayout, now you want set columnweight programmatically. My code is second option, so that if the button have not added to layout, button.getLayoutParams() is null. – mdtuyen Nov 29 '15 at 17:25
0

You can use spec() method to define the columnWeight from java code.

GridLayout.LayoutParams param = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),  GridLayout.spec(GridLayout.UNDEFINED, 1f));

button.setLayoutParams(param);
Rami
  • 7,879
  • 12
  • 36
  • 66