NickitaX
NickitaX

Reputation: 352

Different layouts in Android RecyclerView issue

I'm making a messenger functionality inside app and want to change layout gravity according to some passed value from holder (must be START or END). Everything works fine except gravity. It is set up at START for all holder. Is there any solution?

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Message message = mMessageList.get(position);
    if(message.getSender().equals("System")){
        holder.content.setBackgroundResource(R.drawable.message_bubble_out);
        holder.content.setTextColor(Color.parseColor("#000000"));
        holder.messageLayout.setGravity(Gravity.END);
    }
    holder.content.setText(message.getContent());
}

Message Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/message_wrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/message_content"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:background="@drawable/message_bubble_out"
            android:text="Hi, rangertS!"
            android:textColor="#FFFFFF" />

</LinearLayout>

messageLayout definition:

messageLayout = (LinearLayout) view.findViewById(R.id.message_wrapper);

Upvotes: 0

Views: 80

Answers (1)

Hadi Satrio
Hadi Satrio

Reputation: 4292

Okay, now that you given us a much elaborated explanation for your issue, I think I know the answer.

It seems like gravity is not to blame here.. As I said, your binding logic is on point.

The real culprit here is the way you laid out your message layout. Let's take a look at the lines I point out below:

<LinearLayout
    android:id="@+id/message_wrapper"
    android:layout_width="wrap_content" <--- Use match_parent instead
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/message_content"
            android:layout_width="250dp" <--- Use wrap_content instead
            android:layout_height="wrap_content"
            android:background="@drawable/message_bubble_out"
            android:text="Hi, rangertS!"
            android:textColor="#FFFFFF" />

</LinearLayout>

You can see the two root causes of your problem in the annotated snippet I gave above. Let's elaborate a little more on this:

  1. You're declaring your LinearLayout width to wrap_content meaning gravity will never play a role here. Why? Well because the layout will always be exactly the same size as its content! Try to make your LinearLayout be as width as its parent and you'll see gravity's effect.

  2. 250dp for your TextView is, at least from my POV, a tad too big. There's a little reason to make a TextView bigger than the actual text it contains. Try to use wrap_content here instead.

Hope this helped! :)

Upvotes: 1

Related Questions