Set layout_column and layout_row in GridLayout programmatically
Asked Answered
M

1

16

I have a GridLayout (not GridView) where I want to add some views with a special row and column inex. In XML I can set the View with:

<TextView
    android:id="@+id/textView1"
    android:layout_column="2"
    android:layout_row="4"
    android:text="Large Text" />

But how can I set the attributes layout_column and layout_row programmatically? I want something like this:

GridLayout grid = new GridLayout(getActivity());

grid.setColumn(2);
grid.setRow(4);

grid.addView(new Button(getActivity());
Mesozoic answered 19/5, 2013 at 22:27 Comment(0)
B
18

The equivalent of layout_column and layout_row, as with all layout_... parameters, is to be found as a parameter of a subclass of LayoutParams.

In this case it's GridLayout.LayoutParams, and we use it like this (for a 2x2 grid with a subview in the final row and column, centred within the cell):

gridLayout.setColumnCount(2);
gridLayout.setRowCount(2);

gridLayout.addView(subview, new GridLayout.LayoutParams(
                              GridLayout.spec(1, GridLayout.CENTER),
                              GridLayout.spec(1, GridLayout.CENTER)));
Beamends answered 28/1, 2014 at 5:6 Comment(2)
And how would I move a view to let's say (0,0)?Aestivation
Well wouldn't it just be GridLayout.spec(0, GridLayout.CENTER)? Also kudos for sticking to one question for almost a year and a half!Beamends

© 2022 - 2024 — McMap. All rights reserved.