---
url: https://talkjs.com/docs/UI_Components/Android/Chatbox/
title: "Chatbox | Android | TalkJS Docs"
---

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.

# Chatbox

Display a single conversation.

The `Chatbox` component represents a [chatbox UI](https://talkjs.com/docs/Features/Chat_UIs/#chatbox) for a single conversation, and integrates with the user's TalkJS session.

### Chatbox

fun Chatbox(appId, userId, conversationId, chatHeaderVisible, enterSendsMessage, focusedMessageId, messageFieldPlaceholder, messageFieldSpellcheck, messageFieldVisible, token, baseTheme, themePath, themeCustom, customCssPath, onSendMessage, onBackButtonClick, onDeleteMessage, onFocusMessage, onMessageAction, beforePermissionPrompt, onMissingPermission, modifier, key, controller)

The [TalkJS Chatbox UI.](https://talkjs.com/docs/Features/Chat_UIs/#chatbox) Displays a single conversation.

#### Parameters

**appId**: String

Your app's unique TalkJS ID. You can find it on the Settings page of the [TalkJS dashboard.](https://talkjs.com/dashboard)

**userId**: String

The ID of the current user.

If the given user ID doesn't exist, a "Something went wrong" message will be displayed.

**conversationId _(optional)_**: String?

The ID of the conversation to display.

If the conversation doesn't exist or if the current user isn't a participant of the current conversation, a "Conversation not found" message will be displayed (after a timeout). If you create the conversation in parallel (eg via the REST API or via the Data API), it will show in the chatbox automatically. This means that you can safely point a chatbox at a conversation that might not yet exist. This way the chatbox UI and the conversation can load in parallel, which results in snappier UX.

Passing `null` deselects the current conversation and shows an empty panel. This may be useful for Inbox-like scenarios where you want to keep the chatbox loaded, without showing a conversation.

In the background, the Chatbox keeps active subscriptions to recent conversations, so that switching back and forth between conversations has snappy UX.

**chatHeaderVisible _(optional)_**: Boolean?

Sets whether the chat header is visible.

Defaults to `true`.

**enterSendsMessage _(optional)_**: Boolean?

When true, pressing Enter will send a message.

Defaults to `true`.

**focusedMessageId _(optional)_**: String?

The ID of the currently focused message.

Whenever this value changes to a valid message ID, the relevant message will be scrolled into view.

Also update your local `focusedMessageId` state in `onFocusMessage` so that it stays in sync with the UI's internals.

Note that updating the selected conversation through this parameter will not trigger the `onFocusMessage` callback.

**messageFieldPlaceholder _(optional)_**: String?

Adds custom placeholder text for the message input field.

The default placeholder text is `"Say something..."`

**messageFieldSpellcheck _(optional)_**: Boolean?

Sets whether spellcheck is enabled in the message field.

Defaults to `false`.

**messageFieldVisible _(optional)_**: Boolean?

Sets whether the message input field is visible.

Defaults to `true`.

**token _(optional)_**: String?

A token to authenticate the session with.

See the [Authentication guide](https://talkjs.com/docs/Features/Security/Authentication/) and [Token reference](https://talkjs.com/docs/Features/Security/Advanced_Authentication/#token-reference) for details and examples.

Required when authentication is enabled, otherwise optional.

**baseTheme _(optional)_**: [BaseTheme](https://talkjs.com/docs/UI_Components/Android/Other_types/#BaseTheme)?

The base theme to start from.

Defaults to `DEFAULT`.

**themePath _(optional)_**: String?

Path to a custom theme file, relative to the app's `assets` folder.

**themeCustom _(optional)_**: JsonElement?

Arbitrary custom data passed down to the theme.

Whatever data you pass here will be made available for use in the theme module loaded via `themePath`.

**customCssPath _(optional)_**: String?

Path to a custom CSS file, relative to the app's `assets` folder.

**onSendMessage _(optional)_**: ((event: [SendMessageEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#SendMessageEvent)) -> Unit)?

Callback that triggers when the user sends a message with the TalkJS UI.

`event` is the event object describing the message sending action.

**onBackButtonClick _(optional)_**: ((event: [BackButtonClickEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#BackButtonClickEvent)) -> Unit)?

Fired when the back button in the chat header is clicked.

By default this back button is only shown inside an inbox on small screens. You can change this behavior in the ChatHeader theme.

**onDeleteMessage _(optional)_**: ((event: [DeleteMessageEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#DeleteMessageEvent)) -> Unit)?

Callback that triggers when the user deletes a message with the TalkJS UI.

`event` is the event object describing the message deletion.

**onFocusMessage _(optional)_**: ((event: [FocusMessageEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#FocusMessageEvent)) -> Unit)?

Callback that triggers when a message is focused or unfocused.

`event` is the event object describing the message focus action.

Note that updating the focused message through the `focusedMessageId` parameter will not trigger this callback.

**onMessageAction _(optional)_**: ((event: [MessageActionEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#MessageActionEvent)) -> Unit)?

This event fires when the user clicks an action button or an action Link inside a message.

The event's `action` and `params` fields specify the name of action and its parameters, if any.

See [Action Buttons and Links](https://talkjs.com/docs/Guides/Web_Components/Action_Buttons_Links/) for more info.

**beforePermissionPrompt _(optional)_**: (suspend (request: [PermissionPromptRequest](https://talkjs.com/docs/UI_Components/Android/Other_types/#PermissionPromptRequest)) -> Unit)?

Callback that triggers when the user initiates an action that will require a permissions request from the system, but right before actually requesting the permission from the system.

You can use this callback to inform the user that they will need to grant a system permission in order to perform an action.

`request` is an object describing the permission request.

**onMissingPermission _(optional)_**: ((event: [MissingPermissionEvent](https://talkjs.com/docs/UI_Components/Android/Other_types/#MissingPermissionEvent)) -> Unit)?

Callback that triggers when the user attempts to initiate an action that requires a specific system permission, but that permission was not granted.

`event` is the event object describing the missing permission.

**modifier**: Modifier

**key _(optional)_**: String?

**controller**: [ChatboxController](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController)

A reference to the [ChatboxController](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController).

#### Returns

Unit

### rememberChatboxController

fun rememberChatboxController(): [ChatboxController](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController)

#### Returns

[ChatboxController](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController)

## class ChatboxController

A collection of actions available on the Chatbox UI.

### Method Overview

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__clickBackButton" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">clickBackButton</a></td><td><p>Emits the <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">onBackButtonClick</code> callback of <a class="no-underline [&amp;_code]:text-blue-500 text-blue-500 hover:text-gray-800 transition duration-200 ease-in-out" href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#Chatbox">Chatbox</a>.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__deleteMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">deleteMessage</a></td><td><p>Deletes the message with the given ID.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__editMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">editMessage</a></td><td><p>Edit the message with the given ID.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__focusMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">focusMessage</a></td><td><p>Scrolls to the message with the given messageId and marks it as "focused".</p><p>If <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">null</code> is passed the <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">focusedMessage</code> will be cleared.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__focusMessageField" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">focusMessageField</a></td><td><p>Focus the message field. If it is not mounted yet, it will be focused once the selected conversation has rendered it.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__getMessageFieldText" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">getMessageFieldText</a></td><td><p>Get the plaintext from the message field</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__jumpToNextHighlight" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">jumpToNextHighlight</a></td><td><p>Searches for messages downwards that contains highlighted strings. If a message is found, the UI will scroll that message into view.</p><p>Strings are highlighted when you explicitly call <a class="no-underline [&amp;_code]:text-blue-500 text-blue-500 hover:text-gray-800 transition duration-200 ease-in-out" href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setHighlightedStrings">setHighlightedStrings</a> or when a user types a word/phrase into the <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">ChatSearchBox</code>.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__jumpToPreviousHighlight" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">jumpToPreviousHighlight</a></td><td><p>Searches for a message upwards that contains highlighted strings. If a message is found, the UI will scroll that message into view.</p><p>Strings are highlighted when you explicitly call <a class="no-underline [&amp;_code]:text-blue-500 text-blue-500 hover:text-gray-800 transition duration-200 ease-in-out" href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setHighlightedStrings">setHighlightedStrings</a> or when a user types a word/phrase into the <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">ChatSearchBox</code>.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__sendFileMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">sendFileMessage</a></td><td><p>Send a file message to the current conversation.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__sendLocationMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">sendLocationMessage</a></td><td><p>Send a location message to the current conversation.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__sendMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">sendMessage</a></td><td><p>Send a text message to the current conversation.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setEditing" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">setEditing</a></td><td><p>Enable/disable editing of a given message.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setHighlightedStrings" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">setHighlightedStrings</a></td><td><p>Highlights certain strings in messages</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setMessageFieldText" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">setMessageFieldText</a></td><td><p>Set the plaintext in the message field</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setReferencedMessage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">setReferencedMessage</a></td><td><p>Set/unset the message that's currently being replied to.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setSearchQuery" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">setSearchQuery</a></td><td><p>Sets the query used by the in-conversation search UI.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__toggleReaction" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">toggleReaction</a></td><td><p>Toggle the given emoji reaction for the given message. If the current user already added this reaction, it's removed, and otherwise, it's added.</p><p>Called from the theme's Message component when the user clicks on an existing emoji reaction.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__toggleSearchBox" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">toggleSearchBox</a></td><td><p>Toggles whether the <code class="px-[4px] py-[1.6px] rounded-[4px] bg-gray-200/75 text-gray-700">ChatSearchBox</code> should be visible or not.</p></td></tr></tbody></table>

### constructor

ChatboxController()

### clickBackButton

suspend fun _chatboxController_.clickBackButton()

Emits the `onBackButtonClick` callback of [Chatbox](https://talkjs.com/docs/UI_Components/Android/Chatbox/#Chatbox).

Called from the default ChatHeader theme component for the "back" button which is shown when the chatbox is part of an inbox that's being rendered in mobile mode.

#### Returns

Unit

### deleteMessage

suspend fun _chatboxController_.deleteMessage(messageId)

Deletes the message with the given ID.

#### Parameters

**messageId**: String

#### Returns

Unit

### editMessage

suspend fun _chatboxController_.editMessage(messageId, text, custom)

suspend fun _chatboxController_.editMessage(messageId, content, custom)

Edit the message with the given ID.

#### Parameters

**messageId**: String

**text _(optional)_**: String?

**custom _(optional)_**: Map<String, String?\>?

**content**: List<[SendContentBlock](https://talkjs.com/docs/Data_APIs/Kotlin/Message_Content/#SendContentBlock)\>

#### Returns

Unit

### focusMessage

suspend fun _chatboxController_.focusMessage(messageId): [MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

Scrolls to the message with the given messageId and marks it as "focused".

If `null` is passed the `focusedMessage` will be cleared.

#### Parameters

**messageId _(optional)_**: String?

#### Returns

[MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

The message snapshot if a message with the given ID was found and was scrolled to, or `null` otherwise.

### focusMessageField

suspend fun _chatboxController_.focusMessageField()

Focus the message field. If it is not mounted yet, it will be focused once the selected conversation has rendered it.

#### Returns

Unit

### getMessageFieldText

suspend fun _chatboxController_.getMessageFieldText(): String

Get the plaintext from the message field

#### Returns

String

The text

### jumpToNextHighlight

suspend fun _chatboxController_.jumpToNextHighlight(): [MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

Searches for messages downwards that contains highlighted strings. If a message is found, the UI will scroll that message into view.

Strings are highlighted when you explicitly call [setHighlightedStrings](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setHighlightedStrings) or when a user types a word/phrase into the `ChatSearchBox`.

#### Returns

[MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

The message snapshot if a message was found, or `null` otherwise.

### jumpToPreviousHighlight

suspend fun _chatboxController_.jumpToPreviousHighlight(): [MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

Searches for a message upwards that contains highlighted strings. If a message is found, the UI will scroll that message into view.

Strings are highlighted when you explicitly call [setHighlightedStrings](https://talkjs.com/docs/UI_Components/Android/Chatbox/#ChatboxController__setHighlightedStrings) or when a user types a word/phrase into the `ChatSearchBox`.

#### Returns

[MessageSnapshot](https://talkjs.com/docs/Data_APIs/Kotlin/Messages/#MessageSnapshot)?

The message snapshot if a message was found, or `null` otherwise.

### sendFileMessage

suspend fun _chatboxController_.sendFileMessage(fileToken, custom)

Send a file message to the current conversation.

#### Parameters

**fileToken**: [FileToken](https://talkjs.com/docs/Data_APIs/Kotlin/Message_Content/#FileToken)

**custom _(optional)_**: Map<String, String\>?

#### Returns

Unit

### sendLocationMessage

suspend fun _chatboxController_.sendLocationMessage(location, custom)

Send a location message to the current conversation.

#### Parameters

**location**: [Coordinates](https://talkjs.com/docs/UI_Components/Android/Other_types/#Coordinates)

**custom _(optional)_**: Map<String, String\>?

#### Returns

Unit

### sendMessage

suspend fun _chatboxController_.sendMessage(text, custom, referencedMessage)

suspend fun _chatboxController_.sendMessage(content, custom, referencedMessage)

Send a text message to the current conversation.

#### Parameters

**text**: String

**custom _(optional)_**: Map<String, String\>?

**referencedMessage _(optional)_**: String?

**content**: List<[SendContentBlock](https://talkjs.com/docs/Data_APIs/Kotlin/Message_Content/#SendContentBlock)\>

#### Returns

Unit

### setEditing

suspend fun _chatboxController_.setEditing(messageId)

Enable/disable editing of a given message.

#### Parameters

**messageId _(optional)_**: String?

When `null` is provided, editing mode will be disabled

#### Returns

Unit

### setHighlightedStrings

suspend fun _chatboxController_.setHighlightedStrings(words)

Highlights certain strings in messages

The TalkJS search feature includes the ability to highlight certain strings in messages. Call this method to highlight certain strings without having the user invoke the search feature. Call again with an empty array to disable highlighting.

Note: like the search feature, this option only works on the Growth plan and up.

#### Parameters

**words**: List<String\>

#### Returns

Unit

### setMessageFieldText

suspend fun _chatboxController_.setMessageFieldText(text)

Set the plaintext in the message field

#### Parameters

**text**: String

#### Returns

Unit

### setReferencedMessage

suspend fun _chatboxController_.setReferencedMessage(messageId)

Set/unset the message that's currently being replied to.

#### Parameters

**messageId _(optional)_**: String?

When `null` is provided, the "replying to" message is cleared

#### Returns

Unit

### setSearchQuery

suspend fun _chatboxController_.setSearchQuery(query)

Sets the query used by the in-conversation search UI.

This updates the search box value and highlighted strings at the same time.

#### Parameters

**query**: String

#### Returns

Unit

### toggleReaction

suspend fun _chatboxController_.toggleReaction(messageId, emoji, force)

Toggle the given emoji reaction for the given message. If the current user already added this reaction, it's removed, and otherwise, it's added.

Called from the theme's Message component when the user clicks on an existing emoji reaction.

#### Parameters

**messageId**: String

The ID of the message you want to toggle the reaction on. This message must exist in the current conversation.

**emoji**: String

The emoji you want to toggle. Must be a string of a single unicode emoji glyph, eg `"🎉"`.

**force _(optional)_**: Boolean?

If included, turns the toggle into a one way-only operation. Setting to `true` will add this reaction for the current user and setting to `false` will remove it if it was already added for the current user

#### Returns

Unit

### toggleSearchBox

suspend fun _chatboxController_.toggleSearchBox(force)

Toggles whether the `ChatSearchBox` should be visible or not.

#### Parameters

**force _(optional)_**: Boolean?

If included, turns the toggle into a one way-only operation. Setting to `true` will enable the search box, and setting to `false` will disable it.

#### Returns

Unit

## Examples

The following gives some examples of how to use the `Chatbox` component.

### Create a chatbox with an existing user and conversation

Create a chatbox and pass it an existing user and conversation:

```kotlin
Chatbox(
    appId = "<APP_ID>",
    userId = "sample_user_alice",
    conversationId = "sample_conversation",
)
```

### Create a chatbox with a new user and conversation

The [Kotlin Data API](https://talkjs.com/docs/Data_APIs/Kotlin/), which lets you read, subscribe to, and update your chat data, is included with Android Components.

Import `getTalkSession` into the file where you want to use it:

```kotlin
import com.talkjs.core.getTalkSession
```

Then create the session, user, and conversation:

```kotlin
class ChatViewModel : ViewModel() {
    val appId = "<APP_ID>"
    val userId = "sebastian"
    val conversationId = "my_conversation"

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

        viewModelScope.launch {
            session.currentUser.createIfNotExists(name = "Sebastian")
            session.conversation(conversationId).createIfNotExists()
        }
    }
}
```

Pass the new user and conversation to your chatbox:

```kotlin
val viewModel = viewModel<ChatViewModel>()

Chatbox(
    appId = viewModel.appId,
    userId = viewModel.userId,
    conversationId = viewModel.conversationId,
)
```

### Hide the chat header

Hide the chat header:

```kotlin
Chatbox(
    appId = "<APP_ID>",
    userId = "sample_user_alice",
    conversationId = "sample_conversation",
    chatHeaderVisible = false,
)
```

If you hide the chat header, other UI elements remain visible by default.

### Respond to events

Respond to a sent message event by printing details to the log:

```kotlin
Chatbox(
    appId = "<APP_ID>",
    userId = "sample_user_alice",
    conversationId = "sample_conversation",
    onSendMessage = { event ->
        Log.d("Chat", "Message sent: ${event.message}")
    },
)
```
