> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-rn-guide-message-privately.mintlify.site/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 title="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) { }
  })
  ```
</Accordion>

## Edit a Message

Use `editMessage()` with a [`TextMessage`](/sdk/reference/messages#textmessage) or [`CustomMessage`](/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.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    List<String> tags = new ArrayList<>();
    tags.add("pinned");
    textMessage.setTags(tags);
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val tags: MutableList<String> = ArrayList()
    tags.add("pinned")
    textMessage.setTags(tags)
    ```
  </Tab>
</Tabs>

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

<Tabs>
  <Tab title="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>

  <Tab title="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}")
      }
    })
    ```
  </Tab>
</Tabs>

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.

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

  <Tab title="Kotlin">
    ```kotlin theme={null}
    CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() {
      fun onMessageEdited(message: BaseMessage) {
        Log.d(TAG, "Message Edited")
      }
    })
    ```
  </Tab>
</Tabs>

<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")
  ```
</Warning>

| 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`](/sdk/reference/messages#action) message is created when a message is edited.

For the message edited event, in the [`Action`](/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.
</Note>

***

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Delete Message" icon="trash" href="/sdk/android/delete-message">
    Remove messages from conversations
  </Card>

  <Card title="Send Message" icon="paper-plane" href="/sdk/android/send-message">
    Send text, media, and custom messages
  </Card>

  <Card title="Receive Messages" icon="envelope" href="/sdk/android/receive-messages">
    Handle real-time message events with listeners
  </Card>

  <Card title="Message Structure" icon="list" href="/sdk/android/message-structure-and-hierarchy">
    Understand message types and properties
  </Card>
</CardGroup>
