For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

Android SDK: getting started

Set up Android Components and create your first chat.

View as Markdown

Use an agent

Paste this prompt into your coding agent:

Add TalkJS chat to my app: talkjs.com/SKILL.md

Or set up TalkJS yourself by following the steps below.

Add TalkJS to your Android app

This guide will help you quickly add TalkJS to your Android app and create a chat between two users, using TalkJS Android Components. These are highly customizable chat UI components, built for Jetpack Compose. We'll use them alongside the Kotlin Data API, which we'll use for data manipulation tasks like synchronizing users and conversations.

We'll walk you through adding our libraries to your Gradle build, viewing an existing conversation, creating new users and conversations, customizing the UI, and properly securing it all.

Prerequisites

To make the most of this guide, you will need:

  • A TalkJS account
  • An Android app built with Jetpack Compose, with a minimum SDK of 24 (Android 7.0) or higher, to which you'd like to add TalkJS chat

Installation

To add TalkJS Android Components to your project, include the following in your app module's build.gradle.kts file:

dependencies {
  implementation("com.talkjs:android-components:<latest_version>")
}

Replace <latest_version> with the latest available version, for example 0.1.0. You can check Maven Central for the latest version.

This also includes the Kotlin Data SDK, so you don't need to add it separately.

Permissions

Some features need additional permissions, which you declare in your own AndroidManifest.xml, only if you want to support them:

  • Voice messages: RECORD_AUDIO and MODIFY_AUDIO_SETTINGS
  • Sharing a location: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The system permission prompt will be shown the first time the user performs an action that requires a permission. To show your own explaination before the system prompt, use the beforePermissionPrompt callback of the Chatbox. To handle a denied permission use onMissingPermission.

View a conversation

To view a sample conversation, import Chatbox into the file for the screen to which you'd like to add chat:

import com.talkjs.android.components.Chatbox

Then add the Chatbox component to the screen:

Chatbox(
    modifier = Modifier.fillMaxSize(),
    appId = "<APP_ID>",
    userId = "sample_user_alice",
    conversationId = "sample_conversation",
)

This Chatbox component has the following parameters:

  • appId: Your TalkJS app ID. You can find your app ID on the Settings page of your TalkJS dashboard. For this guide, use the app ID for your test mode, which has built-in sample users and conversations.
  • userId: An identifier for the current user to send messages as. This example uses the ID of an existing sample user, sample_user_alice.
  • conversationId: An identifier of the conversation that you want to view. This example uses the ID for a built-in sample conversation, sample_conversation.

The modifier sets the size of the chatbox. You can modify it to suit your needs.

Run your app. You should get something like the following:

Chatbox showing a message from Sebastian

You now have a fully-featured chat window running in your app. Try sending a message!

To view the conversation as a different user, change the userId. For example, try switching the userId to sample_user_sebastian to view the other side of the sample conversation.

If you don't see the chat window, make sure that you've entered your app ID correctly.

Create new users and conversations

So far in this guide we've used a sample user and conversation. Next, we'll create new users and a conversation between them, and sync them with the TalkJS servers. To do this, we'll use the Kotlin Data API.

Update your Chatbox to look like this:

Chatbox(
    modifier = Modifier.fillMaxSize(),
    appId = "<APP_ID>",
    userId = "frank",
    conversationId = "my_conversation",
)

Then add a ViewModel with the following code, and use it from the screen that contains your Chatbox:

import com.talkjs.core.getTalkSession

class ChatViewModel : ViewModel() {
    init {
        val appId = "<APP_ID>"

        val userId = "frank"
        val otherUserId = "nina"
        val conversationId = "my_conversation"

        val session = getTalkSession(appId = appId, userId = userId)

        viewModelScope.launch {
            session.currentUser.createIfNotExists(name = "Frank")
            session.user(otherUserId).createIfNotExists(name = "Nina")

            val conversation = session.conversation(conversationId)
            conversation.createIfNotExists()
            conversation.participant(otherUserId).createIfNotExists()
        }
    }
}

This code creates a new TalkJS session, which provides access to a continuous, up-to-date flow of your TalkJS data. It then creates two new users and a conversation between them.

Code example

Here's the code so far, so that you can see how it all fits together:

import com.talkjs.android.components.Chatbox
import com.talkjs.core.getTalkSession

class ChatViewModel : ViewModel() {
    init {
        val appId = "<APP_ID>"

        val userId = "frank"
        val otherUserId = "nina"
        val conversationId = "my_conversation"

        val session = getTalkSession(appId = appId, userId = userId)

        viewModelScope.launch {
            session.currentUser.createIfNotExists(name = "Frank")
            session.user(otherUserId).createIfNotExists(name = "Nina")

            val conversation = session.conversation(conversationId)
            conversation.createIfNotExists()
            conversation.participant(otherUserId).createIfNotExists()
        }
    }
}

@Composable
fun ChatScreen() {
    // Creating the ViewModel starts the user and conversation sync
    viewModel<ChatViewModel>()

    Chatbox(
        modifier = Modifier.fillMaxSize(),
        appId = "<APP_ID>",
        userId = "frank",
        conversationId = "my_conversation",
    )
}

Customizing TalkJS

The easiest way to customize TalkJS's components, is using the parameters and callbacks that the components expose. See each component's reference documentation for a list of available parameters and callbacks.

As an example, if you want to hide the built-in chat header, because it doesn't work well with the rest of your UI, you'd render the chatbox like this:

Chatbox(
    appId = "<APP_ID>",
    userId = "frank",
    conversationId = "my_conversation",
    chatHeaderVisible = false,
)

If you need even more flexibility, TalkJS has an extensive theme system, which lets you adapt the styles, markup and behavior of the entire chat UI. See the Customization guide for more details.

To use a custom theme or custom CSS in your app, pass the paths of the files, relative to your app's assets folder, to themePath and customCssPath:

Chatbox(
    appId = "<APP_ID>",
    userId = "frank",
    conversationId = "my_conversation",
    themePath = "my-theme/js/custom-theme.js",
    customCssPath = "my-theme/css/custom.css",
)

Add authentication

Once you're using TalkJS in production you'll need to enable authentication, so that only legitimate users can connect to your chat. You'll need a backend server that can generate tokens for your users to authenticate with. See our Authentication docs for more detail on how to set this up.

Pass the token to getTalkSession for the Kotlin Data API, and to the token parameter of the components:

import com.talkjs.android.components.Chatbox
import com.talkjs.core.getTalkSession

val session = getTalkSession(appId = appId, userId = userId, token = token)

Chatbox(
    appId = appId,
    userId = userId,
    conversationId = conversationId,
    token = token,
)

The token is different each time you connect to TalkJS, so you'll need to call your backend to generate it.

Once you are happy that your chat is loading without errors when you provide a token, go to the Settings page of your dashboard and enable the Authentication (identity verification) option in the Security settings section. When authentication is enabled, only users with a valid token can connect to your chat.

Next steps

For more ways to customize your chat, see our Chatbox reference docs.

To handle push notifications, see our Push notifications guide.