CometChatTextFormatter
TheCometChatTextFormatter abstract class is the base for all formatters. Its constructor takes a char trackingCharacter that triggers the formatter when typed in the composer (e.g., @ for mentions, # for hashtags).
Key Override Methods
| Method | Purpose |
|---|---|
search(Context, String) | Called when the user types after the tracking character. Use this to fetch and display suggestions. |
onScrollToBottom() | Called when the user scrolls to the bottom of the suggestion list. Use for pagination. |
prepareLeftMessageBubbleSpan(Context, BaseMessage, SpannableStringBuilder) | Apply spans to text in received (left) message bubbles. |
prepareRightMessageBubbleSpan(Context, BaseMessage, SpannableStringBuilder) | Apply spans to text in sent (right) message bubbles. |
prepareComposerSpan(Context, BaseMessage, SpannableStringBuilder) | Apply spans to text in the message composer. |
prepareConversationSpan(Context, BaseMessage, SpannableStringBuilder) | Apply spans to the last message preview in the conversations list. |
Pre-Send Hook
OverridehandlePreMessageSend(Context, BaseMessage) to modify a message before it’s sent. This is useful for attaching metadata, transforming text, or adding custom data to the message object.
Suggestion System
The formatter provides a built-in suggestion dropdown:| Method | Description |
|---|---|
setSuggestionItemList(List<SuggestionItem>) | Set the list of suggestions to display |
setShowLoadingIndicator(boolean) | Show/hide a loading spinner in the suggestion dropdown |
setDisableSuggestions(boolean) | Disable the suggestion dropdown entirely |
onItemClick(Context, SuggestionItem, User, Group) | Called when the user selects a suggestion item |
Example: Custom Hashtag Formatter
- Kotlin
- Java
Registering Formatters
Register formatters on a component usingsetTextFormatters:
- Kotlin
- Java
Built-in Formatter: CometChatMentionsFormatter
The UI Kit includesCometChatMentionsFormatter as a built-in formatter that handles @mention detection, user suggestion lists, and spannable highlighting. It serves as a reference implementation for building custom formatters.
See the Mentions Formatter Guide for details.
Global Configuration via DataSource
Formatters can also be configured globally using theDataSource framework. Override getTextFormatters in a DataSourceDecorator to add formatters that apply across all components:
- Kotlin
- Java
Related
- DataSource & ChatConfigurator — Learn the full DataSource decorator framework.
- Mentions Formatter Guide — Built-in mentions formatter reference.
- Customization Overview — See all customization categories.