O
O
OlgaSafronova2020-05-24 23:18:12
Java
OlgaSafronova, 2020-05-24 23:18:12

How to make chat messages appear on different sides?

I'm building a simple chat app using a youtube tutorial, but that tutorial doesn't show how to make messages from different people appear from different angles. The sender and recipient see all messages on the left. I tried using Gravity, but unfortunately it didn't work. Help, please, with the decision of this problem. Thanks in advance.

This is what the application looks like:
5ecad56cac19f962130221.png
Main code:

public class MainActivity extends AppCompatActivity {

private static int SIGN_IN_CODE=1;
private RelativeLayout activity_main;
private FirebaseListAdapter<Message> adapter;
private FloatingActionButton sendBtn;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==SIGN_IN_CODE) {
           if(requestCode == RESULT_OK) {
               Snackbar.make(activity_main, "You are authorized", Snackbar.LENGTH_LONG).show();
               displayAllMessages();
               } else {
                 Snackbar.make(activity_main, "You are not authorized", Snackbar.LENGTH_LONG).show();
                 finish();
           }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity_main=findViewById(R.id.activity_main);
        sendBtn = findViewById(R.id.btnSend);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText textField = findViewById(R.id.messageField);
                if(textField.getText().toString().equals(""))
                    return;
                FirebaseDatabase.getInstance().getReference().push().setValue(
                        new Message(
                                FirebaseAuth.getInstance().getCurrentUser().getEmail(),
                        textField.getText().toString()));
                textField.setText("");
            }
        });

        //Пользователь ещё не авторизован
        if (FirebaseAuth.getInstance().getCurrentUser()==null)
            startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_CODE);
        //Пользователь авторизован
        else {
            Snackbar.make(activity_main, "You are authorized", Snackbar.LENGTH_LONG).show();
            displayAllMessages();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    private void displayAllMessages() {
        ListView listOfMessages = findViewById(R.id.list_of_messages);
        FirebaseListOptions<Message> options =
                new FirebaseListOptions.Builder<Message>()
                        .setQuery(FirebaseDatabase.getInstance().getReference(), Message.class)
                        .setLayout(R.layout.list_item)
                        .build();
         adapter = new FirebaseListAdapter<Message>(options){
                @Override
                protected void populateView(View v, Message model, int position) {
                    TextView mess_user, mess_time;
                    BubbleTextView mess_text;
                    RelativeLayout Layout = v.findViewById(R.id.parentLayout);
                    mess_user = v.findViewById(R.id.message_user);
                    mess_time = v.findViewById(R.id.message_time);
                    mess_text = v.findViewById(R.id.message_text);
                    mess_user.setText(model.getUserName());
                    mess_text.setText(model.getTextMessage());
                    mess_time.setText(DateFormat.format("dd-MM-yyyy HH:mm:ss", model.getMessageTime()));

                   if(!model.getUserName().equals("[email protected]")) {
                   mess_text.setGravity(Gravity.START);
                  } else {
                    mess_text.setGravity(Gravity.END);
                 }
           }
      };
        listOfMessages.setAdapter(adapter);
    }
    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Message class:
public class Message {
    private String UserName;
    private String TextMessage;
    private long MessageTime;

    public Message() {}
    public Message (String UserName, String TextMessage){
        this.UserName = UserName;
        this.TextMessage = TextMessage;

        this.MessageTime = new Date().getTime();
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getTextMessage() {
        return TextMessage;
    }

    public void setTextMessage(String textMessage) {
        TextMessage = textMessage;
    }

    public long getMessageTime() {
        return MessageTime;
    }

    public void setMessageTime(long messageTime) {
        MessageTime = messageTime;
    }
}

Main XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/activity_main">

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/btnSend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/ic_send_button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    app:fabSize="normal">
</com.google.android.material.floatingactionbutton.FloatingActionButton>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@id/btnSend"
    >
    <EditText
        android:id="@+id/messageField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Сообщение..."
        />
</com.google.android.material.textfield.TextInputLayout>

<ListView
    android:id="@+id/list_of_messages"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/text_layout"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:divider="@android:color/transparent"
    android:dividerHeight="12dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll">
</ListView>

</RelativeLayout>

XML for message bubbles:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/message_user"
    android:textStyle="normal|bold"
/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:id="@+id/message_time"
/>

<com.github.library.bubbleview.BubbleTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:id="@+id/message_text"
    android:layout_marginTop="9dp"
    android:layout_below="@id/message_user"
    android:textSize="18sp"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textColor="#fff"
    android:padding="10dp"
    app:angle="10dp"
    app:arrowWidth="8dp"
    app:arrowHeight="10dp"
    app:arrowPosition="10dp"
    app:bubbleColor="#03dac5"
/>

</RelativeLayout>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question