I am inflating a RelativeLayout from XML and then attempting to programmatically sets its layout params and am receiving a ClassCastException.
Code:
The layout params have to be of the type of the parent. So if you are creating a RelativeLayout and adding it to a ListView, the layout params must be of type ListView.LayoutParams, not RelativeLayout.LayoutParams. The correct way to do it is to call inflate this way:
inflate(R.layout.mylayout, theListView, false);
This version of inflate() will correctly inflate the layout parameters (android:layout_*) defined in XML.
If your view is in a ListView, you must use AbsListView.LayoutParams
.
See also: Unable to set margin of item in ListView using custom Adapter