Building your own android chat messenger app similar to Whatsapp using XMPP (Smack 4.1 API) from scratch

– Download the library files for Smack API and GSON which is a library for converting java objects to JSON strings.

– Copy the jar files into the libs folder of your android project. If this folder does not exists then create it.

– Right click on the jar files, go to Build Path -> Add to Build Path. Repeat this step for each .jar file in the libs folder.

Adding third party libraries:

– Download this file and extract it to the src folder of your project directory. This step will add new packages along with your existing packages in the src directory. These packages are required by smack library as third party libraries.

Now we have successfully added all the required library files to our android project.

Create a new java file named MyXMPP.java in your com.marothiatechs.mchat package in src folder.

We will write the implementation of smack library in this file and will use this file to initialise connections to XMPP server and also to send and receive chat messages.

The content of the MyXMPP.java goes as follows:

As you can see in the source code, we have created methods for initialising, connecting, sending messages as well as a listener for processing received messages.

In android long running processes has to be implemented in background. So we will use Android Service for our requirements.

Creating Android Service:

Create a file named LocalBinder.java and copy the below contents:

Now save this file into the package com.marothiatechs.mchat in src directory.

Create another file name MyService.java and copy the code :

MyService.java is the class for android service and it will be running in the background. You can learn more about services here.

Now modify the contents of our MainActivity.java to this:

Now as we have implemented android service and  connection to xmpp server is done inside the service, we have to use our implementation to send messages using xmpp class instead of simply adding an entry to the listview in the chat_layout.xml of Chats.java

So modify the sendTextMessage() method in Chats.java in package com.marothiatechs.fragments to send messages using xmpp. So the final code for Chats.java will be :

Now the java coding part is done and we need to modify our AndroidManifest.xml file  to add certain permissions and to declare MyService.

So the modified AndroidManifest.xml is :

The final project hierarchy should look like this:

Now run the application and go to Chats tab. Try to send any message and it will come back to you because we are sending it to ourself i.e we used same username for both sender and receiver in our code.

The screenshot for the running app:

Simply change the user2 variable in Chats.java to other username if you want to chat with other users.

It was just a simple app to show the implementation of Smack 4.1 API in android. You can further modify the project according to your needs to develop a fully functional messenger.

You can download a demo apk file of the tutorial here.

Leave a Reply

Your email address will not be published. Required fields are marked *