> ## 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.

# Events & Callbacks

> Handle user interaction events, selection mode, and global UI Kit events with component-level and static event listeners.

The UI Kit provides two levels of event handling: component-level callbacks set directly on a View instance, and global events registered via static listener methods on event classes.

## Component-Level Callbacks

### Click Callbacks

| Setter                                              | Triggered When                |
| --------------------------------------------------- | ----------------------------- |
| `setOnItemClick(OnItemClick<Conversation>)`         | User taps a list item         |
| `setOnItemLongClick(OnItemLongClick<Conversation>)` | User long-presses a list item |
| `setOnBackPressListener(OnBackPress)`               | User taps the back button     |
| `setOnSearchClickListener(OnSearchClick)`           | User taps the search icon     |

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    conversations.setOnItemClick { conversation, position ->
        // Navigate to chat screen
        openChat(conversation)
    }

    conversations.setOnItemLongClick { conversation, position ->
        // Show custom context menu
        showCustomMenu(conversation)
    }

    conversations.setOnBackPressListener {
        // Handle back navigation
        finish()
    }

    conversations.setOnSearchClickListener {
        // Open search screen
        openSearch()
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    conversations.setOnItemClick((conversation, position) -> {
        // Navigate to chat screen
        openChat(conversation);
    });

    conversations.setOnItemLongClick((conversation, position) -> {
        // Show custom context menu
        showCustomMenu(conversation);
    });

    conversations.setOnBackPressListener(() -> {
        // Handle back navigation
        finish();
    });

    conversations.setOnSearchClickListener(() -> {
        // Open search screen
        openSearch();
    });
    ```
  </Tab>
</Tabs>

### Error Callback

Use `setOnError` to handle component-level errors:

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    conversations.setOnError { exception ->
        Log.e("Conversations", "Error: ${exception.message}")
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    conversations.setOnError(exception -> {
        Log.e("Conversations", "Error: " + exception.getMessage());
    });
    ```
  </Tab>
</Tabs>

## Selection Mode

Enable single or multi-select on list components:

| Method                                            | Description                                             |
| ------------------------------------------------- | ------------------------------------------------------- |
| `setSelectionMode(UIKitConstants.SelectionMode)`  | Set the selection mode: `NONE`, `SINGLE`, or `MULTIPLE` |
| `setOnSelect(OnSelection<Conversation>)`          | Callback when selection changes                         |
| `getSelectedConversations()`                      | Get the list of currently selected conversations        |
| `clearSelection()`                                | Clear all selections                                    |
| `selectConversation(Conversation, SelectionMode)` | Programmatically select a conversation                  |

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    // Enable multi-select mode
    conversations.setSelectionMode(UIKitConstants.SelectionMode.MULTIPLE)

    // Listen for selection changes
    conversations.setOnSelect { selectedList ->
        updateToolbar(selectedList.size)
    }

    // Get selected items
    val selected = conversations.getSelectedConversations()

    // Clear selection
    conversations.clearSelection()
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    // Enable multi-select mode
    conversations.setSelectionMode(UIKitConstants.SelectionMode.MULTIPLE);

    // Listen for selection changes
    conversations.setOnSelect(selectedList -> {
        updateToolbar(selectedList.size());
    });

    // Get selected items
    List<Conversation> selected = conversations.getSelectedConversations();

    // Clear selection
    conversations.clearSelection();
    ```
  </Tab>
</Tabs>

## Global Events

Global events are fired by the UI Kit when actions happen anywhere in the app. Register listeners using the static listener registration methods on each event class (`addListener`, `addGroupListener`, or `addUserListener` depending on the class).

| Event Class                   | Fires When                                         |
| ----------------------------- | -------------------------------------------------- |
| `CometChatConversationEvents` | Conversation is deleted or updated                 |
| `CometChatMessageEvents`      | Messages are sent, edited, deleted, or reacted to  |
| `CometChatGroupEvents`        | Groups are created, updated, members added/removed |
| `CometChatUserEvents`         | Users are blocked/unblocked                        |
| `CometChatUIEvents`           | UI-level events like opening chat, showing dialogs |
| `CometChatCallEvents`         | Calls are initiated, accepted, rejected, or ended  |

Each listener is registered with a unique `String` tag so it can be removed later.

### Component-Level vs Global Events

* Component-level callbacks (e.g., `setOnItemClick`) are set on a specific View instance and only fire for that component.
* Global events (e.g., `CometChatMessageEvents.addListener`) are static and fire regardless of which component triggered the action. Use them for cross-component coordination.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin theme={null}
    // Register a global message event listener
    CometChatMessageEvents.addListener("unique-listener-id",
        object : CometChatMessageEvents() {
            override fun ccMessageSent(baseMessage: BaseMessage, status: Int) {
                // Handle sent message across the app
            }

            override fun onMessageDeleted(message: BaseMessage) {
                // Handle deleted message
            }
        }
    )

    // Remove the listener when done
    CometChatMessageEvents.removeListener("unique-listener-id")
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    // Register a global message event listener
    CometChatMessageEvents.addListener("unique-listener-id",
        new CometChatMessageEvents() {
            @Override
            public void ccMessageSent(BaseMessage baseMessage, int status) {
                // Handle sent message across the app
            }

            @Override
            public void onMessageDeleted(BaseMessage message) {
                // Handle deleted message
            }
        }
    );

    // Remove the listener when done
    CometChatMessageEvents.removeListener("unique-listener-id");
    ```
  </Tab>
</Tabs>

## Related

* [Events Reference](/ui-kit/android/events) — Full list of all event classes and their methods.
* [Customization Overview](/ui-kit/android/customization-overview) — See all customization categories.
