Skip to content

Instantly share code, notes, and snippets.

View AlexPrestonSB's full-sized avatar

Alex Preston AlexPrestonSB

View GitHub Profile
package com.sendbird.androidchattutorial;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="com.sendbird.androidchattutorial.LoginActivity">
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/reycler_chat"
android:layout_width="0dp"
android:layout_height="0dp"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="wrap_content"
android:paddingTop="8dp">
<ImageView
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="wrap_content"
android:paddingTop="8dp">
<TextView
// Messages sent by others display a profile image and nickname.
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
ImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mSendButton = (Button) findViewById(R.id.button_chat_send);
mMessageEditText = (EditText) findViewById(R.id.edittext_chat);
mRecyclerView = (RecyclerView) findViewById(R.id.reycler_chat);
mLayoutManager = new LinearLayoutManager(this);
@Override
protected void onResume() {
super.onResume();
// Receives messages from SendBird servers
SendBird.addChannelHandler(CHANNEL_HANDLER_ID, new SendBird.ChannelHandler() {
@Override
public void onMessageReceived(BaseChannel baseChannel, BaseMessage baseMessage) {
if (baseChannel.getUrl().equals(mChannelUrl) && baseMessage instanceof UserMessage) {
mChatAdapter.appendMessage((UserMessage) baseMessage);
// Messages sent by me do not display a profile image or nickname.
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
}
// Retrieves 30 most recent messages.
void refresh() {
mChannel.getPreviousMessagesByTimestamp(Long.MAX_VALUE, true, 30, true,
BaseChannel.MessageTypeFilter.USER, null, new BaseChannel.GetMessagesHandler() {
@Override
public void onResult(List<BaseMessage> list, SendBirdException e) {
if (e != null) {
e.printStackTrace();
return;
}