Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set layout_column and layout_row in GridLayout programmatically

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());
like image 631
Cilenco Avatar asked May 19 '13 22:05

Cilenco


People also ask

How to design UI with Android GridLayout programmatically?

Android Gridlayout Example Programmatically 1 GridLayout Example.#N#The GridLayout object in this example has two columns. The first row is a user name row, the user... 2 Design UI With GridLayout Statically#N#Create a new android project with empty activity template, name it as Android... 3 Add Child View To GridLayout Programmatically. More ...

Which is an example of a GridLayout object?

1. GridLayout Example. The GridLayout object in this example has two columns. The first row is a user name row, the user can input the user name in the input text box. The second row is a password row, the user input a password in it. The third row contains only one button, the button merges two columns by setting it’s layout_columnSpan property.

How do I change the size of a row in GridLayout?

You can specify how many columns and rows the grid have with it’s rowCount or columnCount property. You can change the GridLayout object size by specifying it’s layout_margin property. You can even use it’s child view’s layout_rowSpan or layout_columnSpan property to merge nearby rows or columns. 1. GridLayout Example.

How to add a new password row in GridLayout programmatically?

The third row contains only one button, the button merges two columns by setting it’s layout_columnSpan property. When this button is clicked, it will add a new password row in the GridLayout object programmatically. 2. Design UI With GridLayout Statically


1 Answers

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)));
like image 180
Craig McMahon Avatar answered Sep 20 '22 17:09

Craig McMahon