16

I am developing a "match the following" application. The xml used for the application is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lytLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        android:text="Match the following questions with their answers." />

    <GridLayout
        android:id="@+id/lytGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="3"
        android:columnWidth="300dp"
        android:orientation="vertical"
        android:rowCount="5"
        android:stretchMode="columnWidth" >

        <LinearLayout
            android:id="@+id/Q1Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="0"
            android:layout_row="0"
            android:background="@drawable/shape_qn"
            android:gravity="center" >

            <TextView
                android:id="@+id/Q1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Question1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/A1Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="1"
            android:layout_row="0"
            android:background="@drawable/shape"
            android:gravity="center"
            android:text="Answer1" >

            <TextView
                android:id="@+id/A1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Answer1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/Q2Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="0"
            android:layout_row="1"
            android:background="@drawable/shape_qn"
            android:gravity="center" >

            <TextView
                android:id="@+id/Q2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Question2" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/A2Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="1"
            android:layout_row="1"
            android:background="@drawable/shape"
            android:gravity="center" >

            <TextView
                android:id="@+id/A2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Answer2" />
        </LinearLayout>
   </GridLayout>
 </LinearLayout>

I have tried the following code to create the grid layout. However, for the child linear layouts, I am unable to find a way to programmatically set the android:layout_column and android:layout_row parameters.

GridLayout gridLayout = new GridLayout(this);
    gridLayout.setColumnCount(2);
    gridLayout.setRowCount(15);

    LinearLayout lyt1 = new LinearLayout(this);
    LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

Any pointers?

Srikanth
  • 306
  • 1
  • 2
  • 10
  • I have not been able to dig further information. Hence, I am using a table layout instead. – Srikanth Sep 06 '12 at 05:11
  • 12
    I don't undestand why this question has been closed. I think it is an error. It would have helped me if the question was answered. – Softlion Apr 13 '13 at 10:01
  • 4
    var gridLayoutParams = new GridLayout.LayoutParams(); gridLayoutParams.SetGravity(GravityFlags.Fill); gridLayoutParams.RowSpec = GridLayout.InvokeSpec(iRow, 1); gridLayoutParams.ColumnSpec = GridLayout.InvokeSpec(iCol, 1); grid.AddView(aView, gridLayoutParams); – Softlion Apr 13 '13 at 10:07
  • 1
    I would also have found this to be a helpful question to have answered – Crake Jun 06 '13 at 20:28
  • 1
    Same, I would have benefited from an answer. Is there a way to reopen? – Matt Jan 01 '14 at 17:30

2 Answers2

9

Here is how You can create it programmatically:

GridLayout gridLayout = new GridLayout(getContext());

// Add Title
GridLayout.Spec titleTxtSpecColumn = GridLayout.spec(2, GridLayout.BASELINE);
GridLayout.Spec titleRowSpec = GridLayout.spec(0);
TextView titleText = new TextView(getContext());
titleText.setText("Title");

gridLayout.addView(titleText, new GridLayout.LayoutParams(titleRowSpec , titleTxtSpecColumn));

For more details see ApiDemos examples.

Roger Alien
  • 3,040
  • 1
  • 36
  • 46
-3

You should try this :

  gridLayout.setLayoutParams(prmsLinLyt);

and if you want to give values...

LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(90,100);

where layout_width = 90
and layout_height= 100
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Aditya Nikhade
  • 1,373
  • 10
  • 15
  • Thanks I am aware of that. But, I am unable to find an equivalent for android:layout_column and android:layout_row parameters. Those do not show up in the properties of prmsLinLyt. – Srikanth Sep 05 '12 at 12:02