0

Hey guys I am trying to display a custom message inside of my app but I keep getting this error while setting the padding to the layout programaticaly:

Fatal Exception:java.lang.ClassCastExceptionandroid.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

Here is how I try to set the padding:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    if(alignTop){
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getContext().getResources().getDisplayMetrics());
        }
        params.setMargins(0, actionBarHeight, 0,0);
    }else{
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    }
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_START);
    MessageView.this.setLayoutParams(params);

And here is my XML file:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:roboto="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#1e1e1e"
  android:id="@+id/messageViewBackground"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true"
  android:padding="10dp">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/messageViewSpinnerLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">


        <ImageView
            android:id="@+id/messageViewSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_spinner_small"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_spinner_logo_small"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

    <ImageView
        android:id="@+id/messageViewIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@android:drawable/stat_sys_warning"
        android:visibility="gone"/>

    <com.myapp.widgets.RobotoText
        android:id="@+id/messageViewText"
        roboto:font="Light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5B5B5B"
        android:layout_gravity="center_vertical" />

</LinearLayout>
</RelativeLayout>

What am I doing wrong?

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
user5035668
  • 307
  • 1
  • 4
  • 16
  • Try setting FrameLayout.LayoutParams instead if RelativeLayout.LayoutParams. It's in this similar question: http://stackoverflow.com/questions/11544964/framelayout-to-relativelayout-classcastexception-even-if-there-is-no-framelayout – Frank D. Aug 31 '15 at 14:41
  • If you take a look at my code you can see that I am doing similar things to what you suggest and its not working. – user5035668 Aug 31 '15 at 14:46
  • 1
    No, you are still using RelativeLayout.LayoutParms instead of FrameLayout.LayoutParams. Did you read the answers? – Zarwan Aug 31 '15 at 14:50
  • Yeah my bad changed it but but but now the message is set to the top of the screen and I cant set it to be at the bottom – user5035668 Aug 31 '15 at 14:54
  • Another option is to follow this question (have you tried a google search for your problem at all?) http://stackoverflow.com/questions/19604891/java-lang-classcastexception-android-view-viewgrouplayoutparams-cannot-be-cast – Frank D. Aug 31 '15 at 14:55

1 Answers1

0

Just a guess but if MessageView extends FrameLayout that's why the exception might be happening, and you don't need to add the params to MessageView.this, but to the RelativeLayout view, one of the 2 from your layout with id android:id="@+id/messageViewBackground" or android:id="@+id/messageViewSpinnerLayout"

Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48