Skip to content
Advertisement

Android Java Set up recyclerview items that are right aligned

I am developing an android chat app and I would like the messages of the user you are chatting with to be aligned to the right and their messages to be aligned to the left. This is the condition that allows you to distinguish who the message belongs to, I can only write it on the bindview holder:

if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))

But how do I align the item to the right or left?

I have also thought about inflating two different layouts but I should go and operate in the CreateViewHolder in which I would not have the model.getUidSender value. How can I solve this problem?

This is my complete code:

@Override
    protected void onStart() {
        super.onStart();


        CollectionReference collectRefMessage = FirebaseFirestore.getInstance().collection("roomChat")
                .document("rooms")
                .collection(chatId).document("message")
                .collection("messages");

        FirestoreRecyclerOptions<getMessage> options
                = new FirestoreRecyclerOptions.Builder<getMessage>()
                .setQuery(collectRefMessage.limit(1000).orderBy("date", Query.Direction.ASCENDING), getMessage.class)
                .build();

        FirestoreRecyclerAdapter<getMessage, getASetMsg> firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<getMessage, getASetMsg>(options) {
            @Override
            protected void onBindViewHolder(@NonNull @NotNull getASetMsg holder, int position, @NonNull @NotNull getMessage model) {
                holder.txtMsgText.setText(model.getText());
                holder.dateATime.setText(model.getDate());

              //  uidCh = model.getUidSender();
                //if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))


            }

            @NonNull
            @NotNull
            @Override
            public getASetMsg onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {


                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_chat,parent,false);

                getASetMsg viewHolder = new getASetMsg(view);
                return viewHolder;
            }
        };

        rView.setAdapter(firestoreRecyclerAdapter);
        firestoreRecyclerAdapter.startListening();
    }

 public static class getASetMsg extends RecyclerView.ViewHolder {

        TextView txtMsgText, dateATime;

        public getASetMsg(@NonNull View itemView) {
            super(itemView);


            txtMsgText = itemView.findViewById(R.id.txtMsgText);
            dateATime = itemView.findViewById(R.id.dateATime);


        }
    }

Advertisement

Answer

I managed to solve it like this:

 if(model.getUidSender().equals(fAuth.getCurrentUser().getUid())) {
                    final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.messageCard.getLayoutParams();
                    params.gravity = GravityCompat.END; // or GravityCompat.END
                    holder.messageCard.setLayoutParams(params);
                }

So, this is the chat card layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_alignParentEnd="true"
    app:cardBackgroundColor="@color/textC"

    android:layout_marginTop="10dp"
    >

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/messageCard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/textC"
        app:cardCornerRadius="20dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/txtMsgText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi, this is a message"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:textColor="#FFFFFF"
        android:layout_marginTop="25dp"
        android:layout_marginBottom="20dp"
        android:textSize="22dp"/>

    <TextView
        android:id="@+id/dateATime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:text="32/07/2021 00:00"
        android:layout_marginTop="5dp"
        android:textColor="@color/white"
        android:layout_marginLeft="30dp"


        />

    </RelativeLayout>

</com.google.android.material.card.MaterialCardView>

</FrameLayout>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement