I am working on a chat app using Firebase. to display messages, I am using a recyclerView and a FirebaseRecyclerAdapter for it. The problem is that I want to change the layout associated to this FirebaseRecyclerAdapter depending on the sender of the message so that the messages of the sender and receiver looks different, but I don't how to do this.
please if you have any idea let me know ! those are my onStart method and the viewHolder class and the layouts of the messages .
thanks guys.
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Message,MessageViewHolder> FBRA1 = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(
Message.class,
R.layout.singlemessagelayout,
MessageViewHolder.class,
refbase1
) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
try {
viewHolder.setContent(model.getSender().username, model.message);
}catch (NullPointerException e){
}
}
} ;
recyclerView.setAdapter(FBRA1);
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount());
}
public static class MessageViewHolder extends RecyclerView.ViewHolder {
View v ;
public MessageViewHolder(View itemView) {
super(itemView);
v = itemView ;
}
public void setContent(String username, String content) {
TextView messageContent = (TextView)v.findViewById(R.id.emailid2) ;
TextView messagesender = (TextView)v.findViewById(R.id.usernameid2) ;
messageContent.setText(content);
messagesender.setText(username);
}
}
and this is my first message layout called "singlemessagelayout.xml"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/gradient2"
android:layout_margin="10dp"
android:id="@+id/mylayout"
android:layout_alignParentRight="false"
>
<TextView
android:layout_marginLeft="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sender"
android:textColor="#52EFFF"
android:textStyle="italic"
android:textSize="18dp"
android:id="@+id/usernameid2"
/>
<TextView
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="message envoyé"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center"
android:id="@+id/emailid2"
/>
</LinearLayout>
and this is my second message layout called "singlemessagelayout2.xml"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/gradient2"
android:layout_margin="10dp"
android:id="@+id/mylayout"
android:layout_alignParentRight="true"
>
<TextView
android:layout_marginLeft="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sender"
android:textColor="#52EFFF"
android:textStyle="italic"
android:textSize="18dp"
android:id="@+id/usernameid2"
/>
<TextView
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="message envoyé"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center"
android:id="@+id/emailid2"
/>
</LinearLayout>