Start a Basic Chat

Let’s take this tutorial even further by building a simple chat UI and using Mitter.io to send/receive messages.

Build the basic layouts

Before we start handling data, we need a UI to show the outgoing/incoming data. Get started by adding some layouts for your chat bubbles and the chat RecyclerView.

To speed up this tutorial, just copy and paste the following pieces of code to your project. This is very basic Android stuff and we’ll not be diving deep into this part.

back_blue_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="50dp" />
    <solid android:color="@color/colorPrimary" />

</shape>
back_gray_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="50dp" />
    <solid android:color="#EEEEEE" />

</shape>

So, you’ve added some basic layouts for rendering the chat bubbles. Now, let’s style the chat window. Open up your activity_main.xml layout file and add the following code:

Prepare the adapter

Now that your layouts are in place, get started by adding an adapter for your RecyclerView.

Assuming you’ve named your adapter ChatRecyclerViewAdapter, your class should look something like this:

Here, we receive a list of Message objects to display the chat items, and compare the senderId field to the currentUserId field to decide whether the bubble should render on the right or on the left.

Render messages in the window

The final step to rendering the messages in a channel to the screen is to wire up the ChatRecyclerViewAdapter that you created to the RecyclerView that you had already added to your layout.

Get started by initialising an empty list for holding the incoming Message objects and setting a layout manager for your RecyclerView.

Now, you need to make a call to fetch messages in the channel that you had already created.

If you’ve noticed, in the previous code snippet, the channel ID is already defined in the class. Replace the channel ID string with your actual channel ID over there.

Once that’s done, just make a call to the getMessagesInChannel() method on the Messaging object to get a list of messages in the channel:

Here, we add the total incoming message list to our already defined empty message list. Next, we hook up that list with our ChatRecyclerViewAdapter and also retrieve the currently logged-in user ID and pass it to the adapter for the bubble alignment that we discussed previously.

The final step is to assign our ChatRecyclerViewAdapter object as the RecyclerView adapter to render the elements on the screen.

If you open up your app right now, it’ll display a blank screen because you haven’t sent any messages yet.

Basic chat window layout

Let’s do that now.

Send a basic text message

We’ve already added a basic EditText and a Button to our app while defining the layouts. Let’s connect them in the activity to send the typed text on the press of the Send button.

Here, we’re listening to the click events on the Send button and sending a message by calling the sendTextMessage() method on the Messaging object. We pass the typed text to the method as the message text.

After the message has been sent successfully, we clear the input area. You can also show a progress bar while sending the message by utilising this callback.

That’s all wired in. You can test if this works by typing something in the input field and hitting Send.

Do note that you won’t see the message appear on the screen even after it’s sent successfully. This is because we haven’t hooked our push message listener with the UI.

We’ll do this in the next section. For now, after sending a message, just close and reopen the app to see the message populated on the screen.

Messages in the channel being populated in the chat window

Hook push messages with the UI

You can choose any way you want to pass around data from your Application class to your MainActivity. For this tutorial, we’ll stick to an event bus to do our job.

We’ll use Greenrobot’s EventBus 3 for this project. It’s pretty easy to use, while being reliable. Add the library to your project by including this line in your build.gradle:

Once that’s done, add a subscribing method in your MainActivity to listen to the incoming messages:

After that, you need to register your MainActivity to listen to any event bus events. Just modify your onCreate() to include this code:

Then, override the onDestroy() method to clean up any registered listeners:

Finally, navigate to your custom Application class, locate the previously registered push message listener and modify it to this:

If you open up your app now and send a text message, you should be able to see it added to the list as soon as the message is sent.

Messages are being transferred in and out the app in real-time

Last updated