Log in Schedule a demo

How to Build an Android Chat App with CometChat’s Chat Kit

Learn how to use our chat UI kit to build a real-time android chat application in less than 30 minutes.

Nabil Kazi • Apr 21, 2020

Mobile chat applications are transforming and revolutionizing not only the way we communicate, but also the way we do business. Once the exclusive realm of big tech organizations, mobile chat apps are now becoming more commonplace because of the rise of microservices like Chat APIs.

In this android chat and messaging app tutorial, we will be developing an android application with in-app chat using the CometChat Chat UI Kit. The most exciting part is that our chat kits can be easily setup to add chat to any android application in less than 30 minutes.

Now, let’s start working on everything you need to build your own android chat app!

Outlining the App

Your Android Chat App in a nutshell

Before every project, the scope has to be defined. Hence, we have outlined the scope so that we know exactly what should be there in a real-time android chat app and what shouldn’t. Here are a few things that you should consider:

The app should have the following screens -

  • Login & Signup screens

  • A screen to display a list of all the chats

  • Chat message screen to do the actual chatting & display list of all the messages exchanged between in a chat.

  • A screen for the user to select a contact and to start a new chat.

Now that we have agreed on the scope, let’s jump straight into it.

Building the App

I. Setting Up the CometChat SDKs

Before starting with CometChat, it is important to understand how CometChat works! The Key Concepts page of CometChat documentation can help you with that.

Before diving into the code, we’ll need to set up CometChat in the application. CometChat Quick Start document does a wonderful job explaining the steps in detail.

We’ll outline the steps from the above document:

  1. Create your CometChat Pro account

  2. Get your Application API Keys

  3. Add the CometChat Dependency in your Application

  4. Add the CometChat Android UI Kit Library

  5. Add Application API Keys, App ID and region and initialize CometChat SDK‍

Here's how we will initialize the CometChat SDK via the Application class:

II. Developing the Login & Registration Flow

Here's how the Sign Up and Login Screen looks like

In an actual production app, I’d use my custom back-end application or Firebase for user management. Here, we’ll use CometChat’s User Management as a shortcut.

The registration page will ask for the user’s name and contact details. CometChat needs a unique identifier of every user. Here, we’ll consider mobile number as the user’s unique identifier - uid.

Now that we have our user’s name and uid, we’ll use CometChat’s createUser SDK method to create the user in our CometChat’s database. Then, in the final step, we’ll call CometChat’s login SDK method to log the user into our app.

You can also view your registered users in the CometChat Dashboard.‍

Let’s see the entire process in the code below.

We’ll also create a Login screen for already-registered users. Even they would be entering their mobile numbers to log in. We would use CometChat’s login function to accomplish this.

Here’s the code:

Note: I have skipped all the validation part here. The idea is to first complete the solution and  then we add all the error handling and other bells and whistles. But in a production app, do make sure that you add validations on the fields to avoid crashes and unexpected behaviors.

After successful login, users should be routed to the screen which displays a list of all the user’s chats. We’ll call this screen → My Conversations.

You’ll notice that (in the code) on both the screens, in the Login onSuccess method, I am routing the screen to ConversationsActivity. That’s our “My Conversations” screen. We’ll be setting this screen up next.

III. Building Chat Screens

1. Setting Up the Conversation Screen

Here's how the Conversation Screen looks like

Once the user logs in to the app, we would be showing the user’s latest conversations. This screen contains a list of all his one-to-one and group conversations.

For this, we will have to first create an activity - ConversationsActivity.


In our layout file - activity_conversations.xml, add the following snippet to use the CometChatConversationList. Adding this will give you a fully working list of conversations.


CometChatConversationList is a part of CometChat’s Android UI Kit. We won’t have to create RecyclerView, Adapter or any custom development of any sorts to display our conversation list. That’s right! CometChat’s UI Kit handles it all for us.

2. Setting Up User Selection Screen for New Chat

User Selection Screen for New Chat

We need a button on the conversations screen for the user to click and initiate a new chat.

Let’s add it:

And the routing function in the code would be like this:

This User selection screen needs to show a list of all the registered users here. Let’s go ahead and implement the activity to display a list of all the users registered in our app.

In our XML layout file -  activity_select_contact.xml, add the following snippet to use the CometChatUserList. Adding this will give you a fully working list of users.

CometChatUserList is also a part of CometChat’s Android Chat UI Kit. Like CometChatConversationList, we won’t have to create RecyclerView, Adapter or any custom development of any sorts to display our user list. We won’t even have to call the API ourselves!

We have both our Conversations List Screen and New User Selection Screen ready. Now, the final missing piece of the puzzle that’s pending is the actual Chat screen! We need to route the user to this Chat screen when the user clicks on any of the conversation or user.

Let’s get our code rolling and finish it!

3. Implementing the Chat Screen

Here's how the Chat Screen looks like

Once the user clicks on a Conversation or Contact item, we would route the user to CometChat’s Chat Message screen. We would use CometChatMessageListActivity from CometChat’s Android UI Kit library itself. It displays a list of all the messages, images, and the history of the entire chat with that person. It is capable of sending and receiving different types of messages such as text, image, and documents.

No UI development or API calls is needed, which means we can get on with our chat feature.

Like we said earlier, we need to route the user to this Chat screen from both our Conversations List Screen and User Selection Screen. CometChat has a solution for this too — it has ready-made click listener events for both these lists.

Let’s first add it in our Conversations List Screen:

And now, add the same in the User Selection Screen:

That’s it! We have our Chat application ready in less than 30 minutes!

Try it out yourself!

Here’s the repository to check out the complete code and try out the app yourself!

https://github.com/cometchat-pro-tutorials/QuickChat-Android

Steps to run the demo app —

  1. Head to CometChat Pro and create an account

  2. From the dashboard, create a new app and name it whatever you like. For me, it’s "QuickChat"

  3. Once created, click on Explore

  4. Go to the API & Auth Keys tab in the side menu

  5. Copy the App ID, Auth Key & API Key.

  6. Download the repository from the above Github URL.

  7. Replace the App_ID and Auth_Key from the AppConfig file.

  8. Boom! Run the app!

Conclusion

In this tutorial, we built a chat application that leverages CometChat UI Kit for Android and allows users to register and use the application to chat with others. You saw how easy it is to install and set up CometChat and quickly create new screens and implement complex functionalities. Moreover, you learned how easy it is to customize the behavior and build any kind of chat or messaging experience.

I hope this tutorial was helpful and also insightful and I can wait to see how you utilize the great possibilities made by CometChat alongside Android.

Nabil Kazi

CometChat

-

Try out CometChat in action

Experience CometChat's messaging with this interactive demo built with CometChat's UI kits and SDKs.

Try the interactive demo

哆哆女性网大宋第一状元郎宝宝未出生起名吕姓宝宝起名欧菲光股票股吧网上免费婴幼儿起名乙一高贵霸气女宝起名麦振鸿人琴俱亡物联网科技公司起名字碳粉知己是什么意思澄海3c地图下载晗字给男孩起名用诗词起的名字姓柳女孩起名字给宝宝免费起名字怎么起书包公司起名最强会长黑神下雨天生的宝宝起什么名字啊五行属土的起什么名字北京卫视节目单红海行动百度云关于写给团队起名周易起名 免费穿越小说下载网中小学生作文网大染坊续夜深人不静滑雪大冒险破解版周易的宝宝起名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化