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?

5
  • Try setting FrameLayout.LayoutParams instead if RelativeLayout.LayoutParams. It's in this similar question: stackoverflow.com/questions/11544964/… Commented Aug 31, 2015 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. Commented Aug 31, 2015 at 14:46
  • 1
    No, you are still using RelativeLayout.LayoutParms instead of FrameLayout.LayoutParams. Did you read the answers? Commented Aug 31, 2015 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 Commented Aug 31, 2015 at 14:54
  • Another option is to follow this question (have you tried a google search for your problem at all?) stackoverflow.com/questions/19604891/… Commented Aug 31, 2015 at 14:55

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.