Skip to main content
Version: v5

Message Header

Overview

MessageHeader is a Component that showcases the User or Group details in the toolbar. Furthermore, it also presents a typing indicator and a back navigation button for ease of use.

Image

Usage

Integration

You can add MessageHeader component directly into the `layout.xml`` file.

your_layout.xml
<com.cometchat.chatuikit.messageheader.CometChatMessageHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="56dp" />

Actions

Actions dictate how a component functions. They are divided into two types: Predefined and User-defined. You can override either type, allowing you to tailor the behavior of the component to fit your specific needs.

The MessageHeader component does not have any exposed actions.

OnError

This action doesn't change the behavior of the component but rather listens for any errors that occur in the Users component.

YourActivity.java
cometchatMessageHeader.setOnError(cometchatException -> {

});

setOnBackPressListener

OnBackPressListener is triggered when you press the back button in the app bar. It has a predefined behavior; when clicked, it navigates to the previous activity. However, you can override this action using the following code snippet.

YourActivity.java
cometchatMessageHeader.setOnBackPressListener(() -> {

});

Filters

Filters allow you to customize the data displayed in a list within a Component. You can filter the list based on your specific criteria, allowing for a more customized. Filters can be applied using RequestBuilders of Chat SDK.

The MessageHeader component does not have any exposed filters.

Events

Events are emitted by a Component. By using event you can extend existing functionality. Being global events, they can be applied in Multiple Locations and are capable of being Added or Removed.

The MessageHeader component does not produce any events.

Customization

To fit your app's design requirements, you can customize the appearance of the conversation component. We provide exposed methods that allow you to modify the experience and behavior according to your specific needs.

Style

Using Style you can customize the look and feel of the component in your app, These parameters typically control elements such as the color, size, shape, and fonts used within the component.

Image
themes.xml
    <style name="CustomCallButtonStyle" parent="CometChatCallButtonsStyle">
<item name="cometchatCallButtonsVideoCallIconTint">#F76808</item>
<item name="cometchatCallButtonsVoiceCallIconTint">#F76808</item>
</style>

<style name="CustomMessageHeaderStyle" parent="CometChatMessageHeaderStyle">
<item name="cometchatMessageHeaderTitleTextColor">#F76808</item>
<item name="cometchatMessageHeaderAvatarStyle">@style/CustomAvatarStyle</item>
<item name="cometchatMessageHeaderCallButtonsStyle">@style/CustomCallButtonStyle</item>
</style>
cometchatMessageHeader.setStyle(R.style.CustomMessageHeaderStyle);

To know more such attributes, visit the attributes file.

Functionality

These are a set of small functional customizations that allow you to fine-tune the overall experience of the component. With these, you can change text, set custom icons, and toggle the visibility of UI elements.

Following is a list of customizations along with their corresponding code snippets:

PropertyDescriptionCode
UserUsed to pass user object of which header specific details will be shown. This is a one of the required property for the component to function properly..setUser(user)
GroupUsed to pass group object of which header specific details will be shown. This is a one of the required property for the component to function properly..setGroup(Group)
setBackButtonVisibilityUsed to toggle back button visibility.setBackButtonVisibility(View.GONE)
setUserStatusVisibilityUsed to toggle functionality to show user's presence.setUserStatusVisibility(View.VISIBLE)
setVideoCallButtonVisibilityDefines whether a user can initiate a video call.setVideoCallButtonVisibility(View.VISIBLE)
setVoiceCallButtonVisibilityDefines whether a user can initiate a voice call.setVoiceCallButtonVisibility(View.VISIBLE)

Advanced

For advanced-level customization, you can set custom views to the component. This lets you tailor each aspect of the component to fit your exact needs and application aesthetics. You can create and define your views, layouts, and UI elements and then incorporate those into the component.

setDateTimeFormatter

By providing a custom implementation of the DateTimeFormatterCallback, you can configure how time and date values are displayed. This ensures consistent formatting for labels such as "Today", "Yesterday", "X minutes ago", and more.

Each method in the interface 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"

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


cometchatMessageHeader.setDateTimeFormatter(new DateTimeFormatterCallback() {

private final SimpleDateFormat fullTimeFormatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());
private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy", Locale.getDefault());

@Override
public String time(long timestamp) {
return fullTimeFormatter.format(new Date(timestamp));
}

@Override
public String today(long timestamp) {
return "Today";
}

@Override
public String yesterday(long timestamp) {
return "Yesterday";
}

@Override
public String lastWeek(long timestamp) {
return "Last Week";
}

@Override
public String otherDays(long timestamp) {
return dateFormatter.format(new Date(timestamp));
}

@Override
public String minutes(long diffInMinutesFromNow, long timestamp) {
return diffInMinutesFromNow + " mins ago";
}

@Override
public String hours(long diffInHourFromNow, long timestamp) {
return diffInHourFromNow + " hrs ago";
}
});


setAuxiliaryButtonView

Allows adding a custom button or additional action next to the title or trailing section.

Use Cases:

  • Add a Call button (📞) for quick voice/video calls.
  • Include a Block/Report button for moderation.
  • Implement a Pin Chat feature.
YourActivity.java
cometchatMessageHeader.setAuxiliaryButtonView((context, user, group) -> {

});

setItemView

Enables replacing the entire default header with a fully customized ListItem layout.

Use Cases:

  • Create a completely unique message header style.
  • Include additional user details like bio, location, or last seen status.
YourActivity.java
cometchatMessageHeader.setItemView((context, user, group) -> {

});
Image
custom_message_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp">

<ImageView
android:id="@+id/iv_message_header_back"
android:layout_width="@dimen/cometchat_24dp"
android:layout_height="@dimen/cometchat_24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/cometchat_ic_back"
app:tint="@color/black"
tools:ignore="ContentDescription" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/message_header_avatar_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/cometchat_margin_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/message_header_back_icon_layout"
app:layout_constraintTop_toTopOf="parent">

<com.cometchat.chatuikit.shared.views.avatar.CometChatAvatar
android:id="@+id/message_header_avatar_view"
android:layout_width="@dimen/cometchat_40dp"
android:layout_height="@dimen/cometchat_40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout
android:id="@+id/message_header_center_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/cometchat_margin_2"
android:layout_marginEnd="@dimen/cometchat_padding_3"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent">

<TextView
android:id="@+id/tv_message_header_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="start"
android:maxLines="1"
android:text="hvfcghv"
android:textAppearance="?attr/cometchatTextAppearanceHeading4Medium"
android:textColor="?attr/cometchatTextColorPrimary" />


<TextView
android:id="@+id/tv_message_header_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:marqueeRepeatLimit="1"
android:minHeight="@dimen/cometchat_19dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="?attr/cometchatTextAppearanceCaption1Regular"
android:textColor="?attr/cometchatTextColorSecondary" />


</LinearLayout>

<LinearLayout
android:id="@+id/end_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.cometchat.chatuikit.calls.callbutton.CometChatCallButtons
android:id="@+id/call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/cometchat_margin_2" />

</LinearLayout>


</LinearLayout>
cometchatMessageHeader.setItemView((context, user, group) -> {
View view = LayoutInflater.from(context).inflate(R.layout.custom_message_header, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
CometChatAvatar avatar = view.findViewById(R.id.message_header_avatar_view);
TextView titleText = view.findViewById(R.id.tv_message_header_name);
TextView subtitleText = view.findViewById(R.id.tv_message_header_subtitle);
CometChatCallButtons cometChatCallButtons = view.findViewById(R.id.call_button);

if (user != null) {
avatar.setAvatar(user.getName(), user.getAvatar());
titleText.setText(user.getName());
subtitleText.setText(user.getStatus());
cometChatCallButtons.setUser(user);
} else {
avatar.setAvatar(group.getName(), group.getIcon());
titleText.setText(group.getName());
subtitleText.setText(group.getMembersCount() + " members");
cometChatCallButtons.setGroup(group);
}
view.setLayoutParams(params);
return view;
});

setSubtitleView

Allows customizing the subtitle view, usually used for status messages or additional details.

Use Cases:

  • Show the user's last seen or online status.
  • Display a custom typing indicator.
  • Show a custom role or tagline ("Customer Support", "Verified Seller").
YourActivity.java
cometchatMessageHeader.setSubtitleView((context, user, group) -> {

});

Example

Image

You should create a subtitle_layout.xml file and inflate it inside apply function.

YourActivity.java
cometChatMessageHeader.setSubtitleView((context, user, group) -> {
TextView textView = new TextView(context);
if(user!=null) {
textView.setText(user.getStatus());
}else if(group!=null) {
textView.setText(group.getMembersCount() > 1 ? group.getMembersCount() + " members" : group.getMembersCount() + " member" + " • " + group.getDescription());
}
return textView;
});

setLeadingView

Defines a custom leading view, typically used for the receiver's profile picture or avatar.

Use Cases:

  • Display a circular profile picture with a status indicator.
  • Add a custom badge for special roles (Admin, Verified ✅).
YourActivity.java
cometchatMessageHeader.setLeadingView((context, user, group) -> {

});

Example

Image

You need to create a header_leading_view.xml file, inflate it, and then pass it to the .setLeadingView(view) method.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/cometchat_45dp"
android:layout_height="@dimen/cometchat_45dp"
android:orientation="vertical">

<com.cometchat.chatuikit.shared.views.avatar.CometChatAvatar
android:id="@+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<View
android:id="@+id/batch_view"
android:layout_width="match_parent"
android:layout_height="@dimen/cometchat_12dp"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:background="@drawable/admin_batch" />

</RelativeLayout>
cometChatMessageHeader.setLeadingView(new Function3<Context, User, Group, View>() {
@Override
public View apply(Context context, User user, Group group) {
View view = LayoutInflater.from(context).inflate(R.layout.header_leading_view, null);
CometChatAvatar avatar = view.findViewById(R.id.avatar);
View view1 = view.findViewById(R.id.batch_view);
if (user != null) {
avatar.setAvatar(user.getName(), user.getAvatar());
if ("admin".equals(user.getRole())) {
view1.setVisibility(View.VISIBLE);
}
} else {
avatar.setAvatar(group.getName(), group.getIcon());
if (group.getOwner().equals(CometChatUIKit.getLoggedInUser().getUid())) {
view1.setVisibility(View.VISIBLE);
}
}

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.convertDpToPx(context, 45), Utils.convertDpToPx(context, 45));
view.setLayoutParams(params);
return view;
}
});

setTrailingView

Enables customization of the trailing view, typically used for action buttons or indicators.

Use Cases:

  • Add an options menu (⋮) for more actions.
  • Display a mute/unmute toggle.
  • Show a connection strength indicator for calls.
YourActivity.java
cometchatMessageHeader.setTrailingView((context, user, group) -> {

});

Example

Image
cometChatMessageHeader.setTrailingView((context, user, group) -> {
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.save_icon);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.convertDpToPx(context, 24), Utils.convertDpToPx(context, 24));
params.leftMargin = Utils.convertDpToPx(context, 16);
imageView.setLayoutParams(params);
return imageView;
});