I have a RelativeLayout with the xml something like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appHolder"
android:layout_width="210dp"
android:layout_height="100dp"
android:background="@drawable/app_container_box"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
...
</RelativeLayout>
I want to programmatically modify the width of this layout based on some condition.
In the GetView() method, I have:
public View getView(int position, View recycledView, ViewGroup viewGroup) {
View row = recycledView;
Holder holder;
row = inflater.inflate(R.layout.app_icon_tile, viewGroup, false);
if (row == null) {
/*
* I added only the following `if` block
* The rest is older code and was working fine
*/
if (changeWidth) {
row.setLayoutParams(new RelativeLayout.LayoutParams(260, 100));
}
holder = new Holder();
holder.icon = (ImageView) row.findViewById(R.id.gridAppIcon);
holder.appTitle = (TextView) row.findViewById(R.id.gridAppTitle);
row.setTag(holder);
} else {
holder = (Holder) row.getTag();
}
return (row);
}
But when running this code, my application crashes with the following stack trace:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:937)
at android.view.View.measure(View.java:8314)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8314)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
at android.view.View.measure(View.java:8314)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8314)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8314)
at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
at dalvik.system.NativeStart.main(Native Method)
What am I missing?
Your problem lies here:
Because when you set layout params, they should be of parent’s type, not the view’s for which you’re creating params itself.
So for example if your
RelativeLayoutis in aListView, it should getListView.LayoutParams.