I have the following linear layout,and I am setting it as a child to HorizontalScrollView
File menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu"
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent">
<ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none">
</ListView>
</LinearLayout>
File : scrollview.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00ffffff" android:padding="0px"
android:layout_margin="0px" android:fadingEdge="none" android:fadingEdgeLength="0px" android:scrollbars="none">
<LinearLayout android:id="@+id/top" android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="horizontal" android:background="#ffffffff" android:padding="0px" android:layout_margin="0px">
</LinearLayout>
</HorizontalScrollView>
I am trying to set the width of the linear layout programatically but I am getting ClassCastException.(LinearLayout$LayputParams can't be cast to FrameLayout$LayputParams)
LayoutInflater inflater = LayoutInflater.from(this);
scrollView = inflater.inflate(R.layout.scrollview_xml, null);
setContentView(scrollView);
View menu = inflater.inflate(R.layout.menu_xml, null);
scrollView.addView(menu);
scrollView.getChildAt(0).setLayoutParams(new ViewGroup.LayoutParams(100,100));
How to solve this ?
since the scrollView extends a FrameLayout , you need to set the layoutParams of its children to have the FrameLayout's layoutParams .
since it says you have an exception because you set the layoutParams for the wrong layout , use the correct ones :
scrollView.getChildAt(0).setLayoutParams(new FrameLayout.LayoutParams(100,100));
in general , if you have any doubt about the type of the layout , use :
scrollView.getChildAt(0).setLayoutParams(new ViewGroup.LayoutParams(100,100));