Skip to main content
FieldValue
Packagecom.cometchat.chatuikit.shared.formatters
Key classCometChatMentionsFormatter (extends CometChatTextFormatter)
Required setupCometChatUIKit.init() then CometChatUIKit.login("UID")
PurposeFormat @mentions with styled tokens, suggestion list, and click handling for users and group members
Sample appGitHub
RelatedShortCut Formatter | All Guides
CometChatMentionsFormatter extends CometChatTextFormatter to format @mentions in text messages. It styles mention tokens, generates suggestion lists as users type, and handles click events on rendered mentions across the message composer, message bubbles, and conversations list.
CapabilityDescription
Mention formattingAuto-formats mention placeholders into styled spans
Custom stylesColors, fonts, and backgrounds per context (composer, bubbles, conversations)
User and group mentionsWorks with both individual users and group members
Suggestion listGenerates mention candidates from user input
Click handlingListener interface for tap on rendered mentions

Usage

1. Create the formatter

val mentionFormatter = CometChatMentionsFormatter(context)

2. Add to a formatters list

val textFormatters: MutableList<CometChatTextFormatter> = ArrayList()
textFormatters.add(mentionFormatter)

3. Pass to a component

Use setTextFormatters() on CometChatMessageComposer, CometChatMessageList, or CometChatConversations.
messageComposer.setTextFormatters(textFormatters)

Styling Mentions

Mention Click Handling

Set a click listener for mentions in message bubbles:
val mentionFormatter = CometChatMentionsFormatter(context)

mentionFormatter.setOnMentionClick { context, user ->
    Toast.makeText(context, user.name, Toast.LENGTH_SHORT).show()
}

val textFormatters: MutableList<CometChatTextFormatter> = ArrayList()
textFormatters.add(mentionFormatter)
messageComposer.setTextFormatters(textFormatters)

Composer Mention Style

Customize how mentions appear in the message composer input field:
themes.xml
<style name="CustomMessageComposerMentionsStyle" parent="CometChatMessageComposerMentionsStyle">
    <item name="cometchatMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatMentionTextColor">#000000</item>
    <item name="cometchatMentionBackgroundColor">#000000</item>
    <item name="cometchatSelfMentionTextColor">#30A46C</item>
    <item name="cometchatSelfMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatSelfMentionBackgroundColor">#30A46C</item>
</style>
val mentionFormatter = CometChatMentionsFormatter(context)
mentionFormatter.setMessageComposerMentionTextStyle(context, R.style.CustomMessageComposerMentionsStyle)

val textFormatters: MutableList<CometChatTextFormatter> = ArrayList()
textFormatters.add(mentionFormatter)
messageComposer.setTextFormatters(textFormatters)

Message Bubble Mention Style

Customize mentions in incoming and outgoing message bubbles:
themes.xml
<style name="CustomIncomingMessageBubbleMentionStyle" parent="CometChatIncomingBubbleMentionsStyle">
    <item name="cometchatMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatMentionTextColor">#D6409F</item>
    <item name="cometchatMentionBackgroundColor">#D6409F</item>
    <item name="cometchatSelfMentionTextColor">#30A46C</item>
    <item name="cometchatSelfMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatSelfMentionBackgroundColor">#30A46C</item>
</style>

<style name="CustomOutgoingMessageBubbleMentionStyle" parent="CometChatOutgoingBubbleMentionsStyle">
    <item name="cometchatMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatMentionTextColor">#FFFFFF</item>
    <item name="cometchatMentionBackgroundColor">#F9F8FD</item>
    <item name="cometchatSelfMentionTextColor">#30A46C</item>
    <item name="cometchatSelfMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatSelfMentionBackgroundColor">#30A46C</item>
</style>
val mentionFormatter = CometChatMentionsFormatter(context)
mentionFormatter.setOutgoingBubbleMentionTextStyle(context, R.style.CustomOutgoingMessageBubbleMentionStyle)
mentionFormatter.setIncomingBubbleMentionTextStyle(context, R.style.CustomIncomingMessageBubbleMentionStyle)

val textFormatters: MutableList<CometChatTextFormatter> = ArrayList()
textFormatters.add(mentionFormatter)
messageList.setTextFormatters(textFormatters)

Conversations Mention Style

Customize mentions in the conversations list last-message preview:
themes.xml
<style name="CustomConversationsMentionsStyle" parent="CometChatConversationsMentionsStyle">
    <item name="cometchatMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatMentionTextColor">#D6409F</item>
    <item name="cometchatMentionBackgroundColor">#D6409F</item>
    <item name="cometchatSelfMentionTextColor">#30A46C</item>
    <item name="cometchatSelfMentionTextAppearance">?attr/cometchatTextAppearanceBodyRegular</item>
    <item name="cometchatSelfMentionBackgroundColor">#30A46C</item>
</style>
val mentionFormatter = CometChatMentionsFormatter(context)
mentionFormatter.setConversationsMentionTextStyle(context, R.style.CustomConversationsMentionsStyle)

val textFormatters: MutableList<CometChatTextFormatter> = ArrayList()
textFormatters.add(mentionFormatter)
cometChatConversations.setTextFormatters(textFormatters)

Customization Matrix

What you want to changeWhereProperty/APIExample
Maximum number of mentions allowedCometChatMentionsFormattersetMentionLimit(int limit)mentionFormatter.setMentionLimit(5)
Group members fetched for mention suggestionsCometChatMentionsFormatter.setGroupMembersRequestBuilder(...).setGroupMembersRequestBuilder(group -> new GroupMembersRequest.GroupMembersRequestBuilder(group.getGuid()));
Users fetched for mention suggestionsCometChatMentionsFormatter.setUsersRequestBuilder(...).setUsersRequestBuilder(new UsersRequest.UsersRequestBuilder().friendsOnly(true));
Who can be mentionedCometChatMentionsFormatter.setMentionsType(...).setMentionsType(UIKitConstants.MentionsType.USERS_AND_GROUP_MEMBERS)
Where mentions are visibleCometChatMentionsFormatter.setMentionsVisibility(...).setMentionsVisibility(UIKitConstants.MentionsVisibility.BOTH);
Click action on a mentionCometChatMentionsFormattersetOnMentionClickmentionFormatter.setOnMentionClick((context, user) -> { });
Mention text style in composerCometChatMentionsFormattersetMessageComposerMentionTextStyle(context, styleRes)mentionFormatter.setMessageComposerMentionTextStyle(context, R.style.CustomStyle)
Mention text style in outgoing bubblesCometChatMentionsFormattersetOutgoingBubbleMentionTextStyle(context, styleRes)mentionFormatter.setOutgoingBubbleMentionTextStyle(context, R.style.CustomStyle)
Mention text style in incoming bubblesCometChatMentionsFormattersetIncomingBubbleMentionTextStyle(context, styleRes)mentionFormatter.setIncomingBubbleMentionTextStyle(context, R.style.CustomStyle)
Mention text style in conversations listCometChatMentionsFormattersetConversationsMentionTextStyle(context, styleRes)mentionFormatter.setConversationsMentionTextStyle(context, R.style.CustomStyle)

Next Steps