> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit Message

> Edit CometChat text and custom messages after sending them with the Android SDK and update message state in chats.

> [accordion] AI Integration Quick Reference
>
> ```kotlin theme={null}
> // Edit a text message
> val updatedMessage = TextMessage(
>     message.receiverUid, 
>     "Updated text", 
>     message.receiverType
> )
> updatedMessage.id = message.id
>
> CometChat.editMessage(updatedMessage, object: CometChat.CallbackListener<BaseMessage>() {
>     override fun onSuccess(message: BaseMessage) { }
>     override fun onError(e: CometChatException) { }
> })
>
> // Listen for edit events
> CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() {
>     override fun onMessageEdited(message: BaseMessage) { }
> })
> ```

## Edit a Message

Use `editMessage()` with a [`TextMessage`](https://www.cometchat.com/docs/sdk/reference/messages#textmessage) or [`CustomMessage`](https://www.cometchat.com/docs/sdk/reference/messages#custommessage) object. Set the message ID using `setId()`.

### Add/Update Tags

While editing a message, you can update the tags associated with the Message. You can use the `setTags()` method to do so. The tags added while editing a message will replace the tags set when the message was sent.

> [tab] Java
>
> ```java theme={null}
> List<String> tags = new ArrayList<>();
> tags.add("pinned");
> textMessage.setTags(tags);
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val tags: MutableList<String> = ArrayList()
> tags.add("pinned")
> textMessage.setTags(tags)
> ```

Once the message object is ready, call `editMessage()`.

> [tab] Java
>
> ```java theme={null}
> TextMessage updatedMessage = new TextMessage(message.getReceiverUid(), ((TextMessage)message).getText() + "edited",message.getReceiverType());
>
> updatedMessage.setId(message.getId());
>
> CometChat.editMessage(updatedMessage, new CometChat.CallbackListener<BaseMessage>() {
>   @Override
>   public void onSuccess(BaseMessage message) {
>     Log.d(TAG,"Message Edited successfully: "+message.toString());
>   }
>
>   @Override
>   public void onError(CometChatException e) {
>     Log.d(TAG,"Message Edited failed with exception: "+e.getMessage());
>   }
> }); 
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val updatedMessage = TextMessage(message.receiverUid, (message as TextMessage).text + "edited", message.receiverType)
>
> updatedMessage.id=message.id
>
> CometChat.editMessage(updatedMessage, object: CometChat.CallbackListener<BaseMessage>() {
>   override fun onSuccess(message: BaseMessage) {
>     Log.d(TAG,"editMessage onSuccess: ${message}")
>   }
>
>   override fun onError(e: CometChatException) {
>     Log.d(TAG,"editMessage onError: ${e.message}")
>   }
> })
> ```

The object of the edited message will be returned in the `onSucess()` callback method of the listener. The message object will contain the `editedAt` field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The `editedBy` field is also set to the `UID` of the user who edited the message.

| Field | Getter | Return Type | Description |
| - | - | - | - |
| editedAt | `getEditedAt()` | `long` | Timestamp when the message was edited |
| editedBy | `getEditedBy()` | `String` | UID of the user who edited the message |

By default, CometChat allows certain roles to edit a message.

| User Role | Conversation Type | Edit Capabilities |
| - | - | - |
| Message Sender | One-on-one Conversation | Messages they've sent |
| Message Sender | Group Conversation | Messages they've sent |
| Group Owner | Group Conversation | All messages in the group |
| Group Moderator | Group Conversation | All messages in the group |

## Real-time Message Edit Events

Use `onMessageEdited` in `MessageListener` to receive real-time edit events.

> [tab] Java
>
> ```java theme={null}
> CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
>   @Override
>   public void onMessageEdited(BaseMessage message) {
>     Log.d(TAG, "Message Edited");
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() {
>   fun onMessageEdited(message: BaseMessage) {
>     Log.d(TAG, "Message Edited")
>   }
> })
> ```

> [!WARNING]
>
> Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.
>
> ```kotlin theme={null}
> CometChat.removeMessageListener("LISTENER_ID")
> ```

| Field | Getter | Return Type | Description |
| - | - | - | - |
| editedAt | `getEditedAt()` | `long` | Timestamp when the message was edited |
| editedBy | `getEditedBy()` | `String` | UID of the user who edited the message |

## Missed Message Edit Events

When fetching message history, edited messages have `editedAt` and `editedBy` fields set. Additionally, an [`Action`](https://www.cometchat.com/docs/sdk/reference/messages#action) message is created when a message is edited.

For the message edited event, in the [`Action`](https://www.cometchat.com/docs/sdk/reference/messages#action) object received, the following fields can help you get the relevant information-

1. `action` - `edited`
2. `actionOn` - Updated message object with the edited details.
3. `actionBy` - User object containing the details of the user who has edited the message.
4. `actionFor` - User/group object having the details of the receiver to which the message was sent.

> [!NOTE]
>
> In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.

***

***

## Next Steps

> [card] [Delete Message](https://www.cometchat.com/docs/sdk/android/v4/delete-message.md)
>
> Remove messages from conversations

> [card] [Send Message](https://www.cometchat.com/docs/sdk/android/v4/send-message.md)
>
> Send text, media, and custom messages

> [card] [Receive Messages](https://www.cometchat.com/docs/sdk/android/v4/receive-messages.md)
>
> Handle real-time message events with listeners

> [card] [Message Structure](https://www.cometchat.com/docs/sdk/android/v4/message-structure-and-hierarchy.md)
>
> Understand message types and properties


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.