Skip to main content

Where It Fits

CometChatMessageHeader is a header bar component. It sits at the top of a chat screen and displays the avatar, name, and status of the user or group in the conversation. Wire it alongside CometChatMessageList and CometChatMessageComposer to build a complete messaging layout.
ChatActivity.kt

Quick Start

Add the component to your layout XML:
your_layout.xml
Prerequisites: CometChat SDK initialized with CometChatUIKit.init(), a user logged in, and the cometchat-chat-uikit-android dependency added. Set a User or Group on the component — this is required for it to display data:
YourActivity.kt
Or set a group:

Actions and Events

Callback Methods

setOnBackButtonPressed

Fires when the user presses the back button in the header. Default: navigates to the previous activity.
YourActivity.kt
What this does: Overrides the default back-press navigation. When the user taps the back button, your custom logic runs instead.

setOnBackPress

Alternative setter for the back press callback. Functionally identical to setOnBackButtonPressed.
YourActivity.kt

setOnError

Fires on internal errors (network failure, auth issue, SDK exception). This does not change the component’s behavior.
YourActivity.kt
What this does: Registers an error listener. If the component encounters an error, your callback receives the CometChatException.

setNewChatButtonClick

Fires when the new chat button is tapped (visible in AI agent conversations).
YourActivity.kt

setChatHistoryButtonClick

Fires when the chat history button is tapped (visible in AI agent conversations).
YourActivity.kt
  • Verify: After setting an action callback, trigger the corresponding user interaction (back-press) and confirm your custom logic executes instead of the default behavior.

Global UI Events

The CometChatMessageHeader component does not emit any global UI events.

SDK Events (Real-Time, Automatic)

The component listens to these SDK events internally. No manual attachment needed unless additional side effects are required.
SDK ListenerInternal behavior
onUserOnlineUpdates online status indicator for the user
onUserOfflineUpdates offline status indicator for the user
onTypingStartedShows typing indicator in the subtitle area
onTypingEndedHides typing indicator and restores subtitle
onGroupMemberJoinedUpdates group member count
onGroupMemberLeftUpdates group member count
onGroupMemberKickedUpdates group member count
onGroupMemberBannedUpdates group member count
Automatic: user presence, typing indicators, and group member count changes update in real time.

Functionality

Small functional customizations such as toggling visibility of UI elements and setting the user or group object.
MethodsDescriptionCode
setUserPasses a user object to display that user’s header details. Required for the component to function..setUser(user)
setGroupPasses a group object to display that group’s header details. Required for the component to function..setGroup(group)
setBackIconVisibilityToggles visibility for the back button.setBackIconVisibility(View.VISIBLE)
setUserStatusVisibilityToggles visibility for the user’s online/offline status indicator.setUserStatusVisibility(View.GONE)
setGroupStatusVisibilityToggles visibility for the group status indicator.setGroupStatusVisibility(View.GONE)
setVideoCallButtonVisibilityToggles visibility for the video call button.setVideoCallButtonVisibility(View.GONE)
setVoiceCallButtonVisibilityToggles visibility for the voice call button.setVoiceCallButtonVisibility(View.GONE)
setMenuIconVisibilityToggles visibility for the overflow menu icon.setMenuIconVisibility(View.GONE)
setNewChatButtonVisibilityToggles visibility for the new chat button (AI agent conversations).setNewChatButtonVisibility(View.GONE)
setChatHistoryButtonVisibilityToggles visibility for the AI chat history button.setChatHistoryButtonVisibility(View.GONE)
  • Verify: After calling a visibility method, confirm the corresponding UI element is shown or hidden. After calling setUser(user), confirm the header displays that user’s name and avatar.

Custom View Slots

Each slot replaces a section of the default header UI. Slots accept a Function3<Context, User, Group, View> callback that receives the current context, user, and group objects.
SlotMethodReplaces
Item viewsetItemView(Function3)Entire header layout
Leading viewsetLeadingView(Function3)Avatar / left section
Title viewsetTitleView(Function3)Name / title text
Subtitle viewsetSubtitleView(Function3)Subtitle text below name
Trailing viewsetTrailingView(Function3)Right section (action buttons)
Auxiliary buttonsetAuxiliaryButtonView(Function3)Additional action button next to trailing view
OptionssetOptions(List<MenuItem>)Overflow popup menu items

setLeadingView

Replace the avatar / left section.
What this does: Registers a callback that returns a custom View for the leading (left) area of the header. The callback receives the current Context, User, and Group objects.
Create a header_leading_view.xml layout file:
header_leading_view.xml
What this does: Inflates the header_leading_view.xml layout and sets the avatar image from the user or group. If the user has an “admin” role, or if the logged-in user is the group owner, the admin badge view becomes visible. The view is sized to 45dp × 45dp.

setTitleView

Replace the default title view with a custom layout.
What this does: Replaces the default title with a custom TextView that displays the user’s or group’s name.

setSubtitleView

Replace the subtitle text below the user’s or group’s name.
What this does: Registers a callback that returns a custom View for the subtitle area of the header. The callback receives the current Context, User, and Group objects so you can customize the subtitle based on the conversation.
What this does: Creates a TextView and sets its text to the user’s status (for user conversations) or the group’s member count and description (for group conversations). If the group has more than 1 member, it shows “X members”; otherwise it shows “1 member • [description]”.

setTrailingView

Replace the trailing (right) section of the header, used for action buttons or indicators.
What this does: Registers a callback that returns a custom View for the trailing (right) area of the header. The callback receives the current Context, User, and Group objects.
What this does: Creates an ImageView with a save icon drawable, sizes it to 24dp × 24dp with a 16dp left margin, and returns it as the trailing view in the header.

setItemView

Replace the entire default header with a fully customized layout.
What this does: Registers a callback that returns a custom View to replace the entire default message header layout. The callback receives the current Context, User, and Group objects.
Create a custom_message_header.xml layout file:
custom_message_header.xml
What this does: Defines a custom message header layout with a back button, avatar, title and subtitle text views, and call buttons. This layout replaces the entire default header when inflated in setItemView.
Inflate the XML and initialize the views:
What this does: Inflates the custom_message_header.xml layout and populates it with the user’s or group’s avatar, name, and status/member count. If the conversation is with a user, it sets the user’s status as the subtitle and configures call buttons for that user. If the conversation is with a group, it sets the member count as the subtitle and configures call buttons for that group.

setAuxiliaryButtonView

Add a custom button or additional action next to the title or trailing section.
What this does: Registers a callback that returns a custom View to display as an auxiliary button in the header. The callback receives the current Context, User, and Group objects so you can customize the view based on the conversation.

setOptions

Sets the list of popup menu items displayed when the overflow menu icon is tapped.
What this does: Sets custom popup menu items for the header’s overflow menu. Each item has an ID, label, and click action. Setting options automatically makes the menu icon visible.
  • Verify: After setting any custom view slot, confirm the custom view renders in the correct position within the message header, and the data binding populates correctly for the user or group.

Common Patterns

Hide call buttons — text-only chat

Show back button for navigation

Minimal header — hide status and menu

Advanced Methods

setDateTimeFormatter

Provides a custom implementation of DateTimeFormatterCallback to configure how time and date values are displayed. Each method corresponds to a specific case:
  • time(long timestamp) — Custom full timestamp format
  • today(long timestamp) — Called when a message is from today
  • yesterday(long timestamp) — Called for yesterday’s messages
  • lastWeek(long timestamp) — Messages from the past week
  • otherDays(long timestamp) — Older messages
  • minute(long timestamp) / hour(long timestamp) — Exact time unit
  • minutes(long diffInMinutesFromNow, long timestamp) — e.g., “5 minutes ago”
  • hours(long diffInHourFromNow, long timestamp) — e.g., “2 hours ago”
What this does: Overrides the default date/time formatting for the message header. Today’s messages show “Today”, yesterday’s show “Yesterday”, recent messages show “X mins ago” or “X hrs ago”, last week’s show “Last Week”, and older messages show the full date in “dd MMM yyyy” format.

setLastSeenText

Provides a custom implementation for the “last seen” text displayed below the user’s name. Only applies to user conversations.
What this does: Replaces the default last seen text with custom logic. Returns “Active now” for online users and “Last seen recently” for offline users.

setBackIcon

Replaces the default back icon with a completely custom View.

setPopupMenuStyle

Sets a custom style for the overflow popup menu.

AI Agent Button Customization

For AI agent conversations, the header shows new chat and chat history buttons. Customize their icons and tints:
MethodTypeDescription
setNewChatIconDrawableCustom icon for the new chat button
setNewChatIconTint@ColorInt intTint color for the new chat icon
setChatHistoryIconDrawableCustom icon for the chat history button
setChatHistoryIconTint@ColorInt intTint color for the chat history icon

Internal Access

These methods provide direct access to internal components for advanced use cases.
MethodReturnsDescription
getMessageHeaderViewModel()MessageHeaderViewModelThe ViewModel managing header data and state
getAdditionParameter()AdditionParameterData source addition parameter for configurators
getUser()UserThe currently set User object
getGroup()GroupThe currently set Group object
Use these only when the standard API is insufficient. Directly manipulating the ViewModel may conflict with the component’s internal state management.

Style

The component uses XML theme styles. Define a custom style with parent CometChatMessageHeaderStyle in themes.xml, then apply with setStyle().
themes.xml
What this does: Applies the CustomMessageHeaderStyle theme to the CometChatMessageHeader component, changing the title text color, avatar style, and call button icon tints.
To know more such attributes, visit the attributes file.
  • Verify: The header title text displays in orange (#F76808), and the voice and video call icons display with orange tint (#F76808).

Programmatic Style Properties

In addition to XML theme styles, the component exposes programmatic setters for fine-grained control:
MethodTypeDescription
setBackgroundColor@ColorInt intBackground color of the component
setTitleTextColor@ColorInt intTitle text color
setTitleTextAppearance@StyleRes intTitle text appearance
setSubtitleTextColor@ColorInt intSubtitle text color
setSubtitleTextAppearance@StyleRes intSubtitle text appearance
setBackIconTint@ColorInt intTint color for the back icon
setMenuIconTint@ColorInt intTint color for the menu icon
setCornerRadius@Dimension intCorner radius of the component
setAvatarStyle@StyleRes intStyle for the avatar
setStatusIndicatorStyle@StyleRes intStyle for the online/offline status indicator
setTypingIndicatorStyle@StyleRes intStyle for the typing indicator
setCallButtonsStyle@StyleRes intStyle for the call buttons

Customization Matrix

What to changeWhereProperty/APIExample
User objectActivity/FragmentsetUser(User).setUser(user)
Group objectActivity/FragmentsetGroup(Group).setGroup(group)
Override back-press behaviorActivity/FragmentsetOnBackButtonPressed(OnBackPress)setOnBackButtonPressed { ... }
Handle errorsActivity/FragmentsetOnError(OnError)setOnError { e -> ... }
Title text colorthemes.xmlCometChatMessageHeaderStyle with cometchatMessageHeaderTitleTextColor<item name="cometchatMessageHeaderTitleTextColor">#F76808</item>
Avatar stylethemes.xmlCometChatMessageHeaderStyle with cometchatMessageHeaderAvatarStyle<item name="cometchatMessageHeaderAvatarStyle">@style/CustomAvatarStyle</item>
Call button icon tintsthemes.xmlCometChatCallButtonsStyle with cometchatCallButtonsVideoCallIconTint / cometchatCallButtonsVoiceCallIconTint<item name="cometchatCallButtonsVideoCallIconTint">#F76808</item>
Apply a custom styleActivity/FragmentsetStyle(int styleRes)cometchatMessageHeader.setStyle(R.style.CustomMessageHeaderStyle);
Back button visibilityActivity/FragmentsetBackIconVisibility(int).setBackIconVisibility(View.VISIBLE)
User status visibilityActivity/FragmentsetUserStatusVisibility(int).setUserStatusVisibility(View.GONE)
Group status visibilityActivity/FragmentsetGroupStatusVisibility(int).setGroupStatusVisibility(View.GONE)
Video call button visibilityActivity/FragmentsetVideoCallButtonVisibility(int).setVideoCallButtonVisibility(View.GONE)
Voice call button visibilityActivity/FragmentsetVoiceCallButtonVisibility(int).setVoiceCallButtonVisibility(View.GONE)
Menu icon visibilityActivity/FragmentsetMenuIconVisibility(int).setMenuIconVisibility(View.GONE)
New chat button visibilityActivity/FragmentsetNewChatButtonVisibility(int).setNewChatButtonVisibility(View.GONE)
Chat history button visibilityActivity/FragmentsetChatHistoryButtonVisibility(int).setChatHistoryButtonVisibility(View.GONE)
Date/time formattingActivity/FragmentsetDateTimeFormatter(DateTimeFormatterCallback)See setDateTimeFormatter code above
Custom last seen textActivity/FragmentsetLastSeenText(Function2<Context, User, String>)See setLastSeenText code above
Entire header layoutActivity/FragmentsetItemView(Function3)See setItemView code above
Subtitle viewActivity/FragmentsetSubtitleView(Function3)See setSubtitleView code above
Leading view (avatar area)Activity/FragmentsetLeadingView(Function3)See setLeadingView code above
Trailing viewActivity/FragmentsetTrailingView(Function3)See setTrailingView code above
Title viewActivity/FragmentsetTitleView(Function3)See setTitleView code above
Auxiliary button viewActivity/FragmentsetAuxiliaryButtonView(Function3)See setAuxiliaryButtonView code above
Header popup menu optionsActivity/FragmentsetOptions(List<MenuItem>)cometchatMessageHeader.setOptions(menuItems);
Popup menu styleActivity/FragmentsetPopupMenuStyle(int).setPopupMenuStyle(R.style.CustomPopupMenuStyle)

Next Steps

Message List

Display messages in a conversation

Message Composer

Rich input for sending messages

Conversations

Browse recent conversations

Theming

Customize colors, fonts, and styles