Explanation and solution at the bottom.
I am developing one slider layout animation, the animation work fine but when all processes end, they get next E
Try this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.mainview.getWidth(), this.mainview.getHeight());
params.setMargins(left, top, right, bottom);
this.mainview.setLayoutParams(params);
This may be help.
I ran into a similar problem and I found the answer here. Basically, you should choose the LayoutParams depending on the parent, in your case new LinearLayout.LayoutParams
should be new RelativeLayout.LayoutParams
.
Try doing a clean of your project. You must have copy pasted a block of xml from a FrameLayout and changed the tag. This is a nasty bug in android.
There was a similar issue on another discussion. I hope this is the same cause.
You should not make the LayoutParmas
object of the layout whose dimension you want to set from java. Rather you should make the LayoutParams
object of the parent layout. For example in the layout below to set the layout params of the LinearLayout
you have to make the LayoutParmas
of RelativeLayout
not LinearLayout
itself:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".newsblog.NewsDetailsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
In activity class:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30,30); // parent params
linearLayout.setLayoutParmas(params); // child layout
If you want to set the params of the RelativeLayout
, it seems it doesn't have any parent but it has a parent created by android framework which is a frame layout:
FrameLayout.LayoutParmas params = new FrameLayout.LayoutParmas(30, 30); // FrameLayout is a parent-created by android framework
relativeLayout.setLayoutParmas(params); // relative layout is child here
Above error due to passing wrong casting params :- because some time you set programattically parameter for linear layout but when you doing getLayoutParams() then it some time gives parent layout reference then it is gives above error .
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) parentLayout.getLayoutParams();
int leftMargin = (int) getResources().getDimension(R.dimen.exclusion_card_margin_left_right);
int rightMargin = (int) getResources().getDimension(R.dimen.exclusion_card_margin_left_right);
params.setMargins(leftMargin, 0, rightMargin, 0);
parentLayout.setLayoutParams(params);