1

I am using the following code to change the margins in the child grid layout:

GridLayout grid = (GridLayout) findViewById(R.id.grid);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(15,marginTop,15,marginBottom);
grid.setLayoutParams(params);

I am getting error message

android.widget.GridLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

My XML file is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:background="#fff5f5f5"
    tools:context=".LinearLayout" >

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_gravity="center"
        android:columnCount="8"
        android:rowCount="8"
        android:orientation="horizontal">

</GridLayout>

</LinearLayout>

What should I change to get rid of the error?

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
Little Owl
  • 157
  • 3
  • 9

1 Answers1

1

You cannot assign LinearLayout.LayoutParams to GridLayout. Please see this SO answer for an example on how to programmatically adjust GridLayout parameters.

Community
  • 1
  • 1
Kasra
  • 3,045
  • 3
  • 17
  • 34
  • Yeah. I tried it with this line: GridLayout.LayoutParams params = new GridLayout.LayoutParams(). The same error :( – Little Owl Feb 10 '15 at 02:41