Reputation: 157
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?
Upvotes: 1
Views: 3216
Reputation: 3145
You cannot assign LinearLayout.LayoutParams to GridLayout. Please see this SO answer for an example on how to programmatically adjust GridLayout parameters.
Upvotes: 1