Overview
A MessageTemplate provides you with the capability to define and customize both the structure and the behavior of the MessageBubble. It acts as a schema or design blueprint for the creation of a variety of MessageBubble components, allowing you to manage the appearance and interactions of MessageBubble within your application effectively and consistently.Structure

- Leading view: This is where the sender’s avatar is displayed. It’s typically on the left of the MessageBubble for messages from others and on the right for messages from the current user.
- Header view: This displays the sender’s name and is especially useful in group chats where multiple users are sending messages.
- Content view: This is the core of the MessageBubble where the message content (text, images, videos, etc.) is displayed.
- Bottom view: This view can be used to extend the MessageBubble with additional elements, such as link previews or a ‘load more’ button for long messages. It’s typically placed beneath the Content view.
- Footer view: This is where the timestamp of the message and its delivery or read status are displayed. It’s located at the bottom of the MessageBubble.
Properties
MessageTemplate provides you with methods that allow you to alter various properties of the MessageBubble. These properties include aspects such as thetype
and category
of a message, the appearance and behavior of the header, content, and footer sections of the message bubble,
-
Type
Using
setType()
you can set the type of CometChatMessage, This will map your MessageTemplate to the corresponding CometChatMessage. You can set the MessageTemplate Type using the following code snippet. -
Category
Using
.setCategory()
you can set the category of a MessageTemplate. This will create a MessageTemplate with the specified category and link it with a CometChatMessage of the same category. Please refer to our guide on Message Categories for a deeper understanding of message categories. -
Header View
The .
setHeaderView()
method allows you to assign a custom header view to the MessageBubble. By default, it is configured to display the sender’s name.
-
Content View
The
.setContentView()
method allows you to assign a custom content view to the MessageBubble. By default, it displays the Text Bubble, Image Bubble, File Bubble, Audio Bubble, or Video Bubble, depending on the message type.
-
Footer View
The
.setFooterView()
method allows you to assign a custom Footer view to the MessageBubble. By default, it displays the receipt and timestamp.
-
Bottom View
The
.setBottomView()
method allows you to assign a custom Bottom view to the MessageBubble.By defuault is has buttons such as link previews or a ‘load more’ button for long messages.
-
Bubble View
The
.setBubbleView()
method allows you to assign a custom Bubble view to the MessageBubble. By default, headerView, contentView, and footerView together form a message bubble.
- Options
.setOptions()
lets you set the list of actions that a user can perform on a message. This includes actions like reacting to, editing, or deleting a message.
Customization
Let’s dive into how you can use the properties of MessageTemplate to customize an existing template or add a new one to the MessageList component. The First step is to fetch the list of existing templates when you want to modify or add to them. This can be done using the getAllMessageTemplates() method from the DataSource of the CometChatUIKit class.Existing Templates
You will need to first get the MessageTemplate object for the type of message you want to customize. You will be customizing the TextMessage Bubble here. The code snippet to get the Text MessageTemplate is as follows.setTemplates
method.
You can apply MessageTemplates to MessagesList Component using the following code snippet.
Header view
The.setHeaderView()
method of MessageTemplate allows you to add custom views to the header of your message bubbles. In the example below, we will add a custom layout custom_txt_header_layout.xml
to the header view of every text message in the MessageList.

message_template_header_view.xml
Content view
The.setContentView()
method of MessageTemplate allows you to add a custom view to the content of your message bubbles. In the example below, we will add a custom layout custom_message_content_layout.xml
to the content view of every text message in the MessageList.

image_bubble_content_view.xml
Status info view
The.setStatusInfoView()
method of MessageTemplate allows you to add a custom status info to your message bubbles. In the example below, we will add a custom layout status_info_layout.xml
to the footerview and empty layout to the status info view view of every text message in the MessageList.

status_info_layout.xml
status_info_layout.xml
as the footer info view for every message. The bindView() method allows you to bind your custom view, which will be called every time a ViewHolder for that message type is bound.
Bottom View
The.setBottomView()
method of MessageTemplate allows you to add a custom button view to your message bubbles. In the example below, we will add a custom layout custom_message_bottom_layout.xml
to the bottom view of every text message in the MessageList.

message_template_bottom_view.xml
Footer View
The.setFooterView()
method of MessageTemplate allows you to add a footer view to your message bubbles. In the example below, we will add a custom layout custom_menu_layout.xml to the bottom view of every text message in the MessageList.

Bubble View
The .setBubbleView()
method of MessageTemplate allows you to add a bubble view to your message bubbles. In the example below, we will add a custom layout custom_message_content_layout.xml to the bubble view of every text message in the MessageList.

Options List
The.setOptions()
method in the MessageTemplate allows you to customize the options that appear in the action sheet when a message is long-pressed. By default, CometChat UI Kit provides a set of options like “Reply”, “Forward”, “Edit”, and “Delete”.
However, if you wish to override or modify these options, you can use the .setOptions()
method and pass a list of CometChatMessageOption
. This list of options will replace the default set.

New Templates
You can create an entirely new template for custom messages is one of the powerful features of CometChat’s MessageTemplate.
contact_card.xml