1

I have an activity (Chat screen) and a layout file for my messages. If a user is sending the messages, then they should appear on the right hand side of the screen and the message bubble should be green instead of orange (orange is the color of messages you receive). On that ChatScreenActivity the alignment and the colors are fine sometimes, but they can lose their position. Moreover, when you exit ChatScreenActivity and come back to it, some of the messages that should be on the right side will be aligned left (like on the default xml for the message item - like the app suggests that a message you sent is coming from the receiver). I'm failing to find out why this is so. This is my code: In my MessagesAdapter class

    @Override
    protected void onBindViewHolder(@NonNull MessagesAdapter.MessageViewHolder messageViewHolder, int i, @NonNull Messages messages) {
        if (messages.getFrom().equals(currentUser)) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            contentLayout.setLayoutParams(lp);
            messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_green);
            messageViewHolder.profilePicture.setVisibility(View.GONE);
            messageViewHolder.txtUsername.setVisibility(View.GONE);
        } else {
             messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            contentLayout.setLayoutParams(lp);
            messageViewHolder.profilePicture.setVisibility(View.VISIBLE);
            messageViewHolder.txtUsername.setVisibility(View.VISIBLE);
        }
        messageViewHolder.messageText.setText(messages.getMessages());
        try {
            Date date = messages.getTime().toDate();
            String hours = String.valueOf(date.getHours());
            String mins = String.valueOf(date.getMinutes());
            if (mins.equals("0")) {
                mins = "00";
            }
            String time = hours + ":" + mins;
            messageViewHolder.timeText.setText(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And this is my message item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/messConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dp">
    <RelativeLayout
        android:id="@+id/mainRelativeContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_message_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/text_message_body"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@id/image_message_profile"
            android:text="John Doe"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/text_message_body"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/image_message_profile"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/rounded_rectangle_orange"
            android:maxWidth="240dp"
            android:padding="8dp"
            android:text="hi man, how are you?"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/text_message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_message_body"
            android:layout_alignRight="@id/text_message_body"
            android:layout_marginLeft="4dp"
            android:text="11:40"
            android:textSize="10sp" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/image_message_profile"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginLeft="8dp"
            android:src="@drawable/ic_account_circle_white_24dp" />
    </RelativeLayout>

</RelativeLayout>

This is what the program is doing: Image 1:correct alignment but second message should be orange and show picture

Image 2:Both messages aligned left and app says I sent the message that I received

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lape.bthe
  • 31
  • 6
  • I suggest you have two layouts, one for messages on the left and another one for messages on the right. I think that way it would be a little faster too. As a suggestion. – MicroRJ Sep 24 '19 at 15:37
  • I just made one. I'm inflating the layout in `onCreateViewHolder` but I dont have access to the model class so that I can check who the current user is. How can I implement that? – lape.bthe Sep 24 '19 at 15:56
  • If you have extended a RecyclerView you should have plenty of methods to help you accomplish this. https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – MicroRJ Sep 24 '19 at 16:02
  • I'm trying this method as I think it's more efficient and improves performance. However, my recyclerView isn't updating after I send a new message (to show the message that has been sent). Any idea why? It was refreshing just fine before – lape.bthe Sep 25 '19 at 19:02

1 Answers1

2

You need to explicitly set the color to orange in the else case:

 messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);

Because the color is memorized to green. In the second example, you have an orange circle because it was the first.

For your second issue you can check the rules that you have added with getRules() and see if you have added multiple ALIGN rules. In that case you can either use removeRule() to get rid of the unwanted ones, or maintain two layoutParams, one for left and one for right.

Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19