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
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.