I have started to work android application, I designed the layout in such RelativeLayout is the parent of the GridView, I simply want to set the LayoutParams of the GridView programmatically.
Below is my layout and code that How I am doing :
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="510dp"
android:gravity="center"
android:horizontalSpacing="@dimen/image_grid_hspacing"
android:numColumns="@integer/grid_num_cols"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/image_grid_vspacing"
android:background="#FFFFFF" />
</RelativeLayout>
</ScrollView>
Code :
GridView gridview = (GridView) rootView.findViewById (R.id.gridview);
gridview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 300));
When I start my application then it always crashes and error logcat says
Error java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
Sorry for a silly question, I don’t know the reason why it is throwing the exception. Please share your views on the question what could be possibilities for the exception
You are placing only LayoutParams which exists in all ViewGroup Objects. So need to mention clearly to which ViewGroup Object
GridViewbelongs.You change your code as below,