> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-audit-content-webhooks.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Search Messages

> Add CometChat Android UI Kit message search with query input, scoped results, message selection, and navigation to matching chats.

<Accordion title="AI Integration Quick Reference">
  | Field          | Value                                                                                           |
  | -------------- | ----------------------------------------------------------------------------------------------- |
  | Package        | `com.cometchat:chat-uikit-android`                                                              |
  | Key components | `CometChatSearch`, `CometChatMessageList`, `CometChatMessageComposer`, `CometChatMessageHeader` |
  | Purpose        | Full-text message search across conversations with result routing and navigation                |
  | Sample app     | [GitHub](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin)        |
  | Related        | [All Guides](/ui-kit/android/guide-overview)                                                    |
</Accordion>

Search Messages lets users locate specific messages across conversations and groups using keyword search, then navigate directly to the result.

Before starting, complete the [Getting Started](/ui-kit/android/getting-started) guide.

***

## Components

| Component / Class          | Role                                                     |
| :------------------------- | :------------------------------------------------------- |
| `CometChatSearch`          | Main search interface with filter chips and result lists |
| `CometChatMessageList`     | Displays messages in the selected conversation           |
| `CometChatMessageComposer` | Message input for the selected conversation              |
| `CometChatMessageHeader`   | Header bar showing conversation context                  |

***

## Integration Steps

### 1. Add CometChatSearch to your layout

```xml activity_search.xml lines theme={null}
<com.cometchat.chatuikit.search.CometChatSearch
    android:id="@+id/cometchat_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
```

***

### 2. Handle conversation result clicks

When a user taps a conversation result, navigate to the message view for that conversation.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin SearchActivity.kt lines theme={null}
    val search: CometChatSearch = findViewById(R.id.cometchat_search)

    search.setOnConversationClicked { view, position, conversation ->
        val intent = Intent(this, ChatActivity::class.java)
        intent.putExtra("conversation", conversation)
        startActivity(intent)
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java SearchActivity.java lines theme={null}
    CometChatSearch search = findViewById(R.id.cometchat_search);

    search.setOnConversationClicked((view, position, conversation) -> {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("conversation", conversation);
        startActivity(intent);
    });
    ```
  </Tab>
</Tabs>

*File: [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin)*

***

### 3. Handle message result clicks

When a user taps a message result, navigate to the conversation and scroll to that message using `setMessageId()` on the message list.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin SearchActivity.kt lines theme={null}
    search.setOnMessageClicked { view, position, message ->
        val intent = Intent(this, ChatActivity::class.java)
        if (message.receiverType == CometChatConstants.RECEIVER_TYPE_USER) {
            intent.putExtra("uid", (message.receiver as User).uid)
        } else {
            intent.putExtra("guid", (message.receiver as Group).guid)
        }
        intent.putExtra("messageId", message.id)
        startActivity(intent)
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java SearchActivity.java lines theme={null}
    search.setOnMessageClicked((view, position, message) -> {
        Intent intent = new Intent(this, ChatActivity.class);
        if (message.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_USER)) {
            intent.putExtra("uid", ((User) message.getReceiver()).getUid());
        } else {
            intent.putExtra("guid", ((Group) message.getReceiver()).getGuid());
        }
        intent.putExtra("messageId", message.getId());
        startActivity(intent);
    });
    ```
  </Tab>
</Tabs>

*File: [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin)*

***

### 4. Scope search to a specific conversation (optional)

Restrict search results to a single user or group conversation.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin lines theme={null}
    // Scope to a specific user conversation
    search.setUid("alice-uid")

    // Or scope to a specific group conversation
    search.setGuid("group-guid")
    ```
  </Tab>

  <Tab title="Java">
    ```java lines theme={null}
    // Scope to a specific user conversation
    search.setUid("alice-uid");

    // Or scope to a specific group conversation
    search.setGuid("group-guid");
    ```
  </Tab>
</Tabs>

***

### 5. Configure search filters (optional)

Control which filter chips appear and which is selected by default.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin lines theme={null}
    search.setSearchFilters(
        listOf(
            UIKitConstants.SearchFilter.MESSAGES,
            UIKitConstants.SearchFilter.CONVERSATIONS
        )
    )
    search.setInitialSearchFilter(UIKitConstants.SearchFilter.MESSAGES)
    ```
  </Tab>

  <Tab title="Java">
    ```java lines theme={null}
    search.setSearchFilters(
        Arrays.asList(
            UIKitConstants.SearchFilter.MESSAGES,
            UIKitConstants.SearchFilter.CONVERSATIONS
        )
    );
    search.setInitialSearchFilter(UIKitConstants.SearchFilter.MESSAGES);
    ```
  </Tab>
</Tabs>

***

## Feature Matrix

| Feature              | Component / Method                            | File                                                                                             |
| :------------------- | :-------------------------------------------- | :----------------------------------------------------------------------------------------------- |
| Search interface     | `CometChatSearch`                             | [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin) |
| Conversation results | `setOnConversationClicked`                    | [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin) |
| Message results      | `setOnMessageClicked`                         | [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin) |
| Scoped search        | `setUid` / `setGuid`                          | [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin) |
| Filter chips         | `setSearchFilters` / `setInitialSearchFilter` | [SearchActivity](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin) |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Search" icon="magnifying-glass" href="/ui-kit/android/search">
    The search component reference
  </Card>

  <Card title="Message List" icon="messages" href="/ui-kit/android/message-list">
    Display messages in a conversation
  </Card>

  <Card title="All Guides" icon="book" href="/ui-kit/android/guide-overview">
    Browse all feature and formatter guides
  </Card>

  <Card title="Sample App" icon="github" href="https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin">
    Full working sample application on GitHub
  </Card>
</CardGroup>
