0

I am following a tuturiol on youtube to create a chat app. The problem i am facing is that i do not seem to get the chats to be displayed the way whatsapp does, like the sender's should be at the right and the user's at the right. I tried a couple of methods using if else statements seem to align to one side. Here are my codes. And it is not like other questions as i have 2 different xmls and im trying to use java

chat_item_left.xml

<RelativeLayout 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">

<com.github.library.bubbleview.BubbleTextView
    android:layout_alignParentStart="true"
    android:layout_marginTop="5dp"
    android:layout_below="@id/messageUser"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textColor="#FFF"
    android:textSize="18sp"
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    app:arrowWidth="8dp"
    app:angle="8dp"
    app:arrowHeight="10dp"
    app:arrowPosition="14dp"
    app:arrowLocation="left"
    app:bubbleColor="#333639"
    app:arrowCenter="true"
    android:text="hie"
    android:layout_alignParentLeft="true" />

chat_item_right.xml

<RelativeLayout 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">

   <RelativeLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

       <com.github.library.bubbleview.BubbleTextView
           android:layout_alignParentEnd="true"
           android:layout_marginTop="5dp"
           android:textAppearance="@style/TextAppearance.AppCompat.Body1"
           android:textColor="#FFF"
           android:textSize="18sp"
           android:id="@+id/messageText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:padding="10dp"
           app:arrowWidth="8dp"
           app:angle="8dp"
           app:arrowHeight="10dp"
           app:arrowPosition="14dp"
           app:arrowLocation="right"
           app:bubbleColor="@android:color/holo_blue_bright"
           app:arrowCenter="true"
           android:text="hie"
           android:layout_alignParentRight="true" />

And here is what i have done with my code so far

 private void displayChatMessage() {
    ListView listOfMessage = (ListView) findViewById(R.id.listOfMessage);
    Query query = FirebaseDatabase.getInstance().getReference().child("chats");

    FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
            .setLayout(R.layout.chat_item_left)
            .setQuery(query, ChatMessage.class)
            .build();

    adapter = new FirebaseListAdapter<ChatMessage>(options) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            // Bind the Chat to the view
            // ...
            //get references to views of list_item.xml
            TextView messageText ,messageUser,messageTime;
            messageText = (BubbleTextView) v.findViewById(R.id.messageText);


            messageText.setText(model.getMessageText());

        }
    };
    listOfMessage.setAdapter(adapter);
}

Where do i specify to put the right alignemnt?

Tinovimba Mawoyo
  • 400
  • 4
  • 19
  • As a side note Check sender id from `ChatMessage` and then you can set Gravity accordingly by using single list item .. – ADM May 06 '19 at 12:24
  • If you consider at some point to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here you can find a tutorial on how to create a complete and functional [Firestore Chat App](https://www.youtube.com/playlist?list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb). – Alex Mamo May 06 '19 at 12:33
  • use two types of view holder. In your adapter choose the correct view holder according to your message object. (eg: sender/receiver) – Sajith May 06 '19 at 13:08

0 Answers0