Skip to main content
Enable intelligent conversational AI capabilities in your Android app using CometChat UIKit v5 with AI Agent integration:
  • AI Assistant Chat History
  • Chat History Management
  • Contextual Responses
  • Agent Detection
  • Seamless Handoffs
Transform your chat experience with AI-powered assistance that provides intelligent responses and offers seamless integration with your existing chat infrastructure.

Overview

Users can interact with AI agents through a dedicated chat interface that:
  • Provides intelligent responses based on conversation context.
  • Maintains chat history for continuity.
  • Seamlessly integrates with your existing user chat system.
The AI Agent chat interface provides a familiar messaging experience enhanced with AI capabilities, accessible through your main chat flow or as a standalone feature.

Prerequisites

  • Android Studio project with cometchat/cometchat-uikit-android and cometchat/chat-sdk-android in build.gradle.
  • Internet permission in AndroidManifest.xml.
  • Valid CometChat App ID, Region, and Auth Key configured via UIKitSettings.
  • User logged in with CometChatUIKit.login().
  • AI Agent configured in your CometChat dashboard.

Components

Component / ClassRole
AIAssistantChatActivityMain activity for AI agent chat.
CometChatAIAssistantChatHistoryDisplays previous AI conversation history.
CometChatMessageListShows AI messages with threading support.
CometChatMessageComposerInput interface for AI conversations.
CometChatMessageHeaderHeader with AI agent info and controls.

Integration Steps

Step 1 - Activity Setup

Create the AI Assistant chat activity with proper theme and layout configuration.
class AIAssistantChatActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAiAssistantChatBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAiAssistantChatBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val messageJson = intent.getStringExtra(getString(R.string.app_base_message))
        val userJson = intent.getStringExtra(getString(R.string.app_user))

        if (userJson != null && !userJson.isEmpty())
            val user = User.fromJson(userJson)
        if (messageJson != null && !messageJson.isEmpty())
            val parentMessage = BaseMessage.processMessage(JSONObject(messageJson))

        initializeComponents(user, parentMessage)
        initClickListeners()
    }

    private fun initializeComponents(user: User, parentMessage: BaseMessage) {
        binding.messageHeader.user = user // Set user for header
        binding.messageList.user = user // Set user for message list
        binding.messageComposer.user = user // Set user for composer

        if (parentMessage != null) {
            // Set message id of parent message to fetch messages with parent.
            // Here we are setting parent message id to message list to fetch messages and message composer to send reply to that message.
            // Here this is being used for AIAssistantChatHistory
            binding.messageList.setParentMessage(parentMessage!!.getId())
            binding.messageComposer.setParentMessageId(parentMessage!!.getId())
        }

        binding.messageList.setStyle(R.style.CustomCometChatMessageListStyle) // Custom style for AI chat
        binding.messageComposer.style = R.style.CustomMessageComposerStyle // Custom style for AI chat
    }
}
File reference: AIAssistantChatActivity.kt

Step - 2 AIAssistantChatActivity layout:

Add CometChatMessageHeader, CometChatMessageList, and CometChatMessageComposer to your layout to enable a complete AI chat interface. Use the sample XML below as a reference for correct integration.
<!-- activity_ai_assistant_chat.xml -->
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.cometchat.chatuikit.messageheader.CometChatMessageHeader
        android:id="@+id/messageHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.cometchat.chatuikit.messagelist.CometChatMessageList
        android:id="@+id/messageList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.cometchat.chatuikit.messagecomposer.CometChatMessageComposer
        android:id="@+id/messageComposer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

File reference: activity_ai_assistant_chat.xml

Step 3 - Style of Message List & Composer

Define custom styles for the message list and composer to differentiate AI agent chats.

    <style name="CustomMessageComposerStyle">
        <item name="cometchatMessageComposerBackgroundColor">?attr/cometchatBackgroundColor2</item>
        <item name="cometchatMessageComposerComposeBoxStrokeWidth">@dimen/cometchat_1dp</item>
        <item name="cometchatMessageComposerComposeBoxCornerRadius">@dimen/cometchat_radius_2</item>
        <item name="cometchatMessageInputStyle">?attr/cometchatMessageInputStyle</item>
    </style>

    <style name="CustomCometChatMessageListStyle" parent="CometChatMessageListStyle">
        <item name="cometchatMessageListOutgoingMessageBubbleStyle">@style/CustomOutgoingMessageBubbleStyle</item>
    </style>

     <style name="CustomOutgoingMessageBubbleStyle">
         <item name="cometchatMessageBubbleBackgroundColor">?attr/cometchatBackgroundColor4</item>
         <item name="cometchatMessageBubbleCornerRadius">@dimen/cometchat_radius_3</item>
         <item name="cometchatTextBubbleStyle">@style/CustomTextBubbleStyle</item>
         <item name="cometchatMessageBubbleDateStyle">@style/CustomOutgoingMessageDateStyle</item>
     </style>

     <style name="CustomOutgoingMessageDateStyle">
         <item name="cometchatDateTextAppearance">?attr/cometchatTextAppearanceCaption2Regular</item>
         <item name="cometchatDateTextColor">?attr/cometchatTextColorSecondary</item>
     </style>

     <style name="CustomTextBubbleStyle">
         <item name="cometchatTextBubbleTextColor">?attr/cometchatTextColorPrimary</item>
         <item name="cometchatTextBubbleLinkPreviewBackgroundColor">?attr/cometchatNeutralColor400</item>
         <item name="cometchatTextBubbleLinkPreviewLinkAppearance">?attr/cometchatTextAppearanceCaption1Regular</item>
         <item name="cometchatTextBubbleLinkPreviewDescriptionColor">?attr/cometchatNeutralColor900</item>
         <item name="cometchatTextBubbleLinkPreviewTitleAppearance">?attr/cometchatTextAppearanceBodyBold</item>
         <item name="cometchatTextBubbleLinkPreviewTitleColor">?attr/cometchatNeutralColor900</item>
         <item name="cometchatTextBubbleLinkPreviewDescriptionAppearance">?attr/cometchatTextAppearanceCaption1Regular</item>
         <item name="cometchatTextBubbleLinkPreviewLinkColor">?attr/cometchatNeutralColor900</item>
         <item name="cometchatTextBubbleLinkPreviewCornerRadius">@dimen/cometchat_radius_2</item>
         <item name="cometchatTextBubbleTextLinkColor">?attr/cometchatInfoColor</item>
     </style>

Step 4 - Initialize click listeners

Initialize click listeners of message header to handle new chat creation and chat history access.

private fun initClickListeners() {

    // New chat creation
    binding.messageHeader.setNewChatButtonClick {
        Utils.hideKeyBoard(this@AIAssistantChatActivity, binding.root)
        val intent = Intent(this@AIAssistantChatActivity, AIAssistantChatActivity::class.java)
        intent.putExtra(getString(R.string.app_user), user.toJson().toString()) // Pass user to create new chat
        startActivity(intent)
        finish()
    }

    // Chat history access
    binding.messageHeader.setChatHistoryButtonClick {
        val intent = Intent(this@AIAssistantChatActivity, AIAssistantChatHistoryActivity::class.java)
        intent.putExtra(getString(R.string.app_user), user.toJson().toString()) // Pass user to fetch chat history
        startActivity(intent)
    }
}

Step 5 - Create an activity for AIAssistantChatHistory component.

Create a new activity to host CometChatAIAssistantChatHistory component and handle its interactions.
class AIAssistantChatHistoryActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAiAssistantChatHistoryBinding
    private var user: User? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAiAssistantChatHistoryBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val userJson = intent.getStringExtra(getString(R.string.app_user))

        if (userJson != null && !userJson.isEmpty()) {
            user = User.fromJson(userJson)

            // Set user to fetch chat history
            binding.cometchatAiAssistantChatHistory.setUser(user)
        }

        // Use setStyle() method of the component to apply custom styles if needed
        // binding.cometchatAiAssistantChatHistory.setStyle(R.style.CustomCometChatAIAssistantChatHistoryStyle)
        // See docs of CometChatAIAssistantChatHistory for available style attributes

        // init click listeners
        initClickListeners()
    }

        private fun initClickListeners() {
            // History item click
            binding.cometchatAiAssistantChatHistory.setOnItemClickListener { view, position, message ->
                val appEntity = message.getReceiver()
                    if (appEntity is User) {
                        user = appEntity
                        val intent = Intent(this@AIAssistantChatHistoryActivity, AIAssistantChatActivity::class.java)
                        intent.putExtra(getString(R.string.app_user), appEntity.toJson().toString())
                        intent.putExtra(
                            getString(R.string.app_base_message),
                            message.getRawMessage().toString()
                        )
                        startActivity(intent)
                        finish()
                    }
            }

            // New chat creation from history screen
            binding.cometchatAiAssistantChatHistory.setNewChatButtonClick {
                val intent = Intent(this@AIAssistantChatHistoryActivity, AIAssistantChatActivity::class.java)
                intent.putExtra(getString(R.string.app_user), user!!.toJson().toString()) // Pass user to create new chat
                startActivity(intent)
                finish()
            }

            // Close history screen
            binding.cometchatAiAssistantChatHistory.setCloseButtonClick {
                // finish the activity
            }
        }
}

Step 6 - AIAssistantChatActivity layout:

Add CometChatAIAssistantChatHistory to your layout to enable access to AI chat history. Use the sample XML below as a reference for correct integration.
<!-- activity_ai_assistant_chat_history.xml -->
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <com.cometchat.chatuikit.aiassistantchathistory.CometChatAIAssistantChatHistory
            android:id="@+id/cometchat_ai_assistant_chat_history"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

Step 7 - Launching AI Chat

Create intent and start AI Assistant chat from your main application.
// In your main activity or chat list
fun launchAIAssistantChat(aiUser: User, parentMessage: BaseMessage? = null) {
    val intent = Intent(this, AIAssistantChatActivity::class.java)
    intent.putExtra(getString(R.string.app_user), aiUser.toJson().toString())
    startActivity(intent)
}

Implementation Flow Summary

StepAction
1User selects AI agent from chat list
2AIAssistantChatActivity launches
3Parse intent data and detect agent chat (Role of user must be “@agentic”)
4Initialize UI with AI-specific styling
6Configure chat history and navigation
7Launch chat with AI agent

Customization Options

  • Custom AI Assistant Empty Chat View: Customize the empty state view using setAIAssistantEmptyChatGreetingView() method.
  • Streaming Speed: Adjust AI response streaming speed via setStreamingSpeed() method.
  • AI Assistant Suggested Messages: Create custom list of suggested messages and set quick prompts using setAIAssistantSuggestedMessages() method.
  • AI Assistant Tools: Set tools for the AI agent using setAIAssistantTools() method.

Feature Matrix

FeatureImplementationUI Component
AI Chat InterfaceAIAssistantChatActivityFull chat screen
Chat HistoryCometChatAIAssistantChatHistoryChat history screen

Android AI Builder Sample

Explore this feature in the CometChat AI Builder: GitHub → AI Builder

Android Sample App (Java)

Explore this feature in the CometChat SampleApp: GitHub → SampleApp