Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout is returning LinearLayout LayoutParams

Here is a snippet from my layout xml that defines my RelativeLayout.

<RelativeLayout
    android:id="@+id/npvirrButtons"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    >
    <Button
        android:id="@+id/buttonNPVLabel"
        android:layout_marginLeft="20dp"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

    <Button
        android:id="@+id/buttonIRR_NFV"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

</RelativeLayout>

Here is where I save the reference to it.

this.npvirrButtons = (RelativeLayout)this.findViewById(R.id.npvirrButtons);

Then later when I try to change the height from 0dp to wrap_content, I grab the layout params like this:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.npvirrButtons.getLayoutParams();

But I get an exception thrown:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    at com.mydomain.myapp.MyActivity.calculateIRR(MyActivity.java:564)

Why is my RelativeLayout returning a LinearLayout params object?

like image 624
Kenny Wyland Avatar asked Jun 07 '15 02:06

Kenny Wyland


1 Answers

getLayoutParams() returns the current LayoutParameters of the view according to its parent. So, buttonIRR_NFV will return RelativeLayout.LayoutParameter.

Think about it this way: getLayoutParams() is a method you call to alter the current view's position on its container. If you want to modify the way you layout one of your children, you have to get its LayoutParameters instead.

like image 184
MLProgrammer-CiM Avatar answered Oct 19 '22 23:10

MLProgrammer-CiM