Skip to main content
Text formatters let you process message text with tracking characters, suggestion lists, and spannable transformations. Use them to add hashtag detection, custom mentions, link previews, or any text pattern processing. Both modules have their own CometChatTextFormatter class with the same API pattern:
  • Kotlin XML: com.cometchat.uikit.kotlin.shared.formatters.CometChatTextFormatter
  • Jetpack Compose: com.cometchat.uikit.compose.presentation.shared.formatters.CometChatTextFormatter

CometChatTextFormatter API

The abstract class takes a trackingCharacter that triggers the formatter when typed in the composer (e.g., @ for mentions, # for hashtags).

Key Override Methods

MethodPurpose
search(context, queryString)Called when the user types after the tracking character. Fetch and display suggestions.
onScrollToBottom()Called when the user scrolls to the bottom of the suggestion list. Use for pagination.
prepareLeftMessageBubbleSpan(context, message, spannable)Apply spans to text in incoming message bubbles.
prepareRightMessageBubbleSpan(context, message, spannable)Apply spans to text in outgoing message bubbles.
prepareComposerSpan(context, message, spannable)Apply spans to text in the message composer.
prepareConversationSpan(context, message, spannable)Apply spans to the last message preview in the conversation list.
handlePreMessageSend(context, message)Modify a message before it’s sent (attach metadata, transform text).
onItemClick(context, suggestionItem, user, group)Called when the user selects a suggestion item.

Suggestion System

MethodDescription
setSuggestionItemList(items)Set the list of suggestions to display
setShowLoadingIndicator(show)Show/hide a loading spinner in the suggestion dropdown
setDisableSuggestions(disable)Disable the suggestion dropdown entirely

Example: Custom Hashtag Formatter

import com.cometchat.uikit.kotlin.shared.formatters.CometChatTextFormatter
import com.cometchat.uikit.kotlin.shared.formatters.SuggestionItem

class HashtagFormatter : CometChatTextFormatter('#') {

    override fun search(context: Context, queryString: String?) {
        val suggestions = fetchHashtags(queryString)
        setSuggestionItemList(suggestions)
    }

    override fun onScrollToBottom() {
        // Load more suggestions
    }

    override fun prepareLeftMessageBubbleSpan(
        context: Context,
        baseMessage: BaseMessage,
        spannable: SpannableStringBuilder
    ): SpannableStringBuilder? {
        applyHashtagSpans(spannable, context)
        return spannable
    }

    override fun prepareRightMessageBubbleSpan(
        context: Context,
        baseMessage: BaseMessage,
        spannable: SpannableStringBuilder
    ): SpannableStringBuilder? {
        applyHashtagSpans(spannable, context)
        return spannable
    }
}

Registering Formatters

val hashtagFormatter = HashtagFormatter()
conversations.setTextFormatters(listOf(hashtagFormatter))

// Or on message composer / message list
messageComposer.setTextFormatters(listOf(hashtagFormatter))
messageList.setTextFormatters(listOf(hashtagFormatter))

Built-in Formatter: CometChatMentionsFormatter

The UI Kit includes CometChatMentionsFormatter as a built-in formatter that handles @mention detection, user suggestion lists, and spannable highlighting. It’s automatically added to components when mentions are enabled. Each module has its own implementation:
  • Kotlin XML: com.cometchat.uikit.kotlin.shared.formatters.CometChatMentionsFormatter
  • Jetpack Compose: com.cometchat.uikit.compose.presentation.shared.formatters.CometChatMentionsFormatter
See the Mentions Formatter Guide for details.