Search code examples
androidandroid-listviewandroid-linearlayoutandroid-relativelayout

Android, RelativeLayout in Adapter is causing ERROR


I am trying to make a chat bubble, I have successfully did it with a text only, but when i want to add another text to put the time inside the bubble i had to add a RelativeLayout to put it on the actual linear layout, but this time i am getting an error.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:transitionGroup="true">


    <RelativeLayout
        android:id="@+id/my_lay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:text="test"
            android:background="@drawable/sender_message_9"/>
        <TextView
            android:id="@+id/messageTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="12:12 PM"
            android:layout_alignBottom="@id/message"
            android:layout_alignRight="@id/message"
            />
    </RelativeLayout>

</LinearLayout>

ADAPTER:

public class ChatAdapter extends ArrayAdapter<MessageProvider> {
    private List<Message> list = new ArrayList<>();
    private TextView TXT;
    private RelativeLayout rLayout;
    Context context;

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.source_message, parent, false);
        }

        ...

        TXT.setText(Message);

        //Set Linear Changes
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(!POSITION){ // Moves the layout to the right
            params.gravity = Gravity.RIGHT;
        }else { // Moves it to the Left
            params.gravity = Gravity.LEFT;
        }
        TXT.setLayoutParams(params);
        return convertView;


    }
}

ERROR:

05-05 00:10:44.563    8962-8962/com.example.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

I would appreciate any answer, thanks.


Solution

  • change the following line

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    

    to

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    

    because you have included the textview inside a RelativeLayout parent.