andrewedgar
andrewedgar

Reputation: 877

Gravity not set properlywith LayoutParams in onBindViewHolder

I have a chat activity that is arranging messages based on the username of the sender. All of the behavior outlined in onBindViewHolder is working properly, except for the Gravity of the container (all messages are aligning end when they should be aligning start and end for partner and logged in user, respectively). What have I done wrong in the implementation of LayoutParams?

Here is my code:

@Override
public void onBindViewHolder(@NonNull DMViewHolder holder, int position) {

    // This method fills fields with data for each list item


    Message message = messageList.get(position);

    String signedInUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

    boolean isSignedInUser = message.getAuthorUID().equals(signedInUserID);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");

    holder.message.setText(message.getMessage());
    holder.date.setText(simpleDateFormat.format(message.getTimestamp()));


    if (isSignedInUser) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_END);
        holder.singleDMContainer.setLayoutParams(params);

        holder.textContainer.setBackgroundResource(R.drawable.user_message_text_background);
    } else {
        RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params2.addRule(RelativeLayout.ALIGN_START);
        holder.singleDMContainer.setLayoutParams(params2);

        holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
    }


}

and the xml file for the messages:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/singleDMContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">


    <RelativeLayout
        android:id="@+id/text_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/user_message_text_background">

        <TextView
            android:id="@+id/dm_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_extralight"
            android:gravity="start"
            android:padding="5dp"
            android:text="@string/sample_message"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dm_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toEndOf="@+id/dm_message"
            android:fontFamily="@font/nunito_extralight"
            android:padding="5dp"
            android:text="@string/sample_time"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="normal" />

    </RelativeLayout>


</RelativeLayout>

Upvotes: 0

Views: 124

Answers (1)

atarasenko
atarasenko

Reputation: 1808

Apply the rule to the child view, not to the container: replace

holder.singleDMContainer.setLayoutParams(params2);

with

holder.textContainer.setLayoutParams(params2);

UPDATE:

Also use RelativeLayout.ALIGN_PARENT_START instead of RelativeLayout.ALIGN_START, and RelativeLayout.ALIGN_PARENT_END instead of RelativeLayout.ALIGN_END.

Upvotes: 1

Related Questions