How to change the background drawable of a layout in java?

Viewed 393

I am trying to change the background of message item in a chat app but instead of changing the background of the message item it changes everything around the message on its line. I have tried other solutions to this problem but they all end up like below.

Example:

enter image description here

Here is the code for the spawning of the item:

 mUserMsg = new FirebaseRecyclerAdapter<ChatMessage, ViewHolder>(ChatMessage.class,R.layout.chat_item_sent,ViewHolder.class,mRef)
    {
        @Override
        protected void populateViewHolder(ViewHolder view, ChatMessage message, int position) {
            LinearLayout messageLine = view.layout;
            TextView messgaeText = view.message;
            TextView senderText = view.sender;
            ImageView leftPic = view.leftPic;
            View mView= view.fucker;


            if (message.getUserID().equals(USER_ID))
            {
                messageLine.setGravity(Gravity.END);
                leftPic.setVisibility(View.GONE);

                messageLine.setBackground(ContextCompat.getDrawable(mView.getContext(),R.drawable.round_corners_sent));

                messgaeText.setText(message.getMessageText());
                senderText.setText(message.getUserID());
            }
            else
            {
                messageLine.setGravity(Gravity.LEFT);

                Picasso.with(getApplicationContext()).load(Uri.parse(picURL)).fit().centerCrop().into(leftPic);


                messgaeText.setText(message.getMessageText());
                senderText.setText(message.getUserID());
            }

        }
    };

    recyclerView.setLayoutManager(mLinearLayoutManager);
    recyclerView.setAdapter(mUserMsg);

}

This is the drawable I am trying to change to:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>

    <stroke
        android:width="40dp"
        android:color="#a8bbcf"/>

    <solid android:color="#a8bbcf"/>
</shape>

Here is the chat item layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/messageLine"
    android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/leftMessagePic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/individMessageLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/round_corners_received"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp">

        <ImageView
            android:id="@+id/imageMessage"

            android:layout_width="100dp"
            android:layout_height="100dp"
            android:visibility="gone" />


        <TextView
            android:id="@+id/messageTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="200dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="normal|bold"
            tools:text="Message" />

        <TextView
            android:id="@+id/senderTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textColor="@color/black"
            android:textSize="12sp"
            tools:text="Sender" />

    </LinearLayout>

</LinearLayout>
0 Answers
Related