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

# Message Privately

> Start private one-to-one chats from CometChat Android UI Kit group conversations with message options, user lookup, and navigation.

<Accordion title="AI Integration Quick Reference">
  | Field          | Value                                                                                                                                                                                              |
  | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | Package        | `com.cometchat:chat-uikit-android`                                                                                                                                                                 |
  | Key components | `CometChatMessageList`, `CometChatMessageComposer`, `CometChatMessageHeader`, `CometChat.sendMessage()`                                                                                            |
  | Purpose        | Allow users to start a private one-on-one chat from a user profile or list screen.                                                                                                                 |
  | Related        | [Message List](/ui-kit/android/message-list), [Message Composer](/ui-kit/android/message-composer), [Message Header](/ui-kit/android/message-header), [All Guides](/ui-kit/android/guide-overview) |
</Accordion>

Allow users to initiate a private one-on-one chat from another user’s profile or list screen using CometChat’s Android UI Kit.

## Overview

The **Message Privately** feature streamlines direct messaging by enabling:

* Quick entry into a one-on-one conversation from a user context.
* Automatic conversation creation if none exists.
* Optional initial message send to surface the chat in lists.

Users tap **Message Privately** → launch `MessagesActivity` with the target user’s UID → chat UI loads.

## Prerequisites

* Android Studio project with CometChat Android UI Kit v5 (`com.cometchat:chat-uikit-android`) added to `build.gradle`.
* Valid CometChat **App ID**, **Auth Key**, and **Region** initialized.
* `<uses-permission android:name="android.permission.INTERNET"/>` in `AndroidManifest.xml`.
* Users created in your CometChat app.
* User must be logged in via `CometChatUIKit.login()` before invoking this feature.
* Existing `MessagesActivity` capable of handling one-on-one chats.

## Components

| Component / Class         | Responsibility                                                       |
| :------------------------ | :------------------------------------------------------------------- |
| `UserDetailsActivity`     | Shows user profile UI and **Message Privately** button.              |
| `MessagesActivity`        | Chat screen for one-on-one conversation with a `User`.               |
| `MainActivity`            | (Optional) entry point for sending initial message programmatically. |
| `CometChatUIKit.login()`  | Authenticates the current user session.                              |
| `CometChat.sendMessage()` | Sends a dummy text message to initialize conversation.               |

## Integration Steps

### 1. Launch One-on-One Chat from Profile

Navigate from `UserDetailsActivity` to `MessagesActivity` with the selected user.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin lines theme={null}
    // In UserDetailsActivity.kt
    binding.messagePrivatelyBtn.setOnClickListener {
        val intent = Intent(this, MessagesActivity::class.java)
        intent.putExtra(getString(R.string.app_user), Gson().toJson(user))
        startActivity(intent)
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java lines theme={null}
    // In UserDetailsActivity.java
    binding.messagePrivatelyBtn.setOnClickListener(v -> {
      Intent intent = new Intent(this, MessagesActivity.class);
      intent.putExtra(getString(R.string.app_user), new Gson().toJson(user));
      startActivity(intent);
    });
    ```
  </Tab>
</Tabs>

**File reference:**\
[`UserDetailsActivity.java`](https://github.com/cometchat/cometchat-uikit-android/blob/v5/sample-app-java/src/main/java/com/cometchat/sampleapp/java/ui/activity/UserDetailsActivity.java)

### 2. Handle Incoming Intent in Chat Screen

Deserialize the `User` JSON extra and configure the chat UI.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin lines theme={null}
    // In MessagesActivity.kt
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_messages)

        val userJson = intent.getStringExtra(getString(R.string.app_user))
        if (userJson != null) {
            val user = Gson().fromJson(userJson, User::class.java)
            messageHeader.setUser(user)
            messageList.setUser(user)
            composer.setUser(user)
        }
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java lines theme={null}
    // In MessagesActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_messages);

      String userJson = getIntent().getStringExtra(getString(R.string.app_user));
      if (userJson != null) {
        User user = new Gson().fromJson(userJson, User.class);
        messageHeader.setUser(user);
        messageList.setUser(user);
        composer.setUser(user);
      }
    }
    ```
  </Tab>
</Tabs>

**File reference:**\
[`MessagesActivity.java`](https://github.com/cometchat/cometchat-uikit-android/blob/v5/sample-app-java/src/main/java/com/cometchat/sampleapp/java/ui/activity/MessagesActivity.java)

### 3. (Optional) Programmatically Create Conversation

Send an initial greeting to ensure the chat appears in conversation lists.

<Tabs>
  <Tab title="Kotlin">
    ```kotlin lines theme={null}
    // In MainActivity.kt
    val txtMessage = TextMessage(
        "cometchat-uid-2",
        "👋",
        CometChatConstants.RECEIVER_TYPE_USER
    )
    CometChat.sendMessage(txtMessage, object : CometChat.CallbackListener<BaseMessage>() {
        override fun onSuccess(msg: BaseMessage) {
            Log.d("SendMsg", "Initial message sent.")
        }
        override fun onError(e: CometChatException?) {
            Log.e("SendMsg", e?.message ?: "Error sending message")
        }
    })
    ```
  </Tab>

  <Tab title="Java">
    ```java lines theme={null}
    // In MainActivity.java
    txtMessage = new TextMessage(
      "cometchat-uid-2",
      "👋",
      CometChatConstants.RECEIVER_TYPE_USER
    );
    CometChat.sendMessage(txtMessage, new CometChat.CallbackListener<BaseMessage>() {
      @Override public void onSuccess(BaseMessage msg) {
        Log.d("SendMsg", "Initial message sent.");
      }
      @Override public void onError(CometChatException e) {
        Log.e("SendMsg", e.getMessage());
      }
    });
    ```
  </Tab>
</Tabs>

**File reference:**\
[`MainActivity.java`](https://github.com/cometchat/cometchat-uikit-android/blob/v5/sample-app-java/src/main/java/com/cometchat/sampleapp/java/ui/activity/MainActivity.java)

## Implementation Flow

| Step | Action                                               | Location                               |
| :--- | :--------------------------------------------------- | :------------------------------------- |
| 1    | Tap **Message Privately** button                     | `UserDetailsActivity.java`             |
| 2    | Launch `MessagesActivity` with user JSON extra       | `UserDetailsActivity` onClick listener |
| 3    | Deserialize and bind `User` to UI Kit components     | `MessagesActivity.onCreate()`          |
| 4    | (Optional) Send initial message to surface chat list | `MainActivity.sendInitialMessage()`    |

## Customization Options

* **Button Text & Style:** Update `messagePrivatelyBtn` in `activity_user_details.xml`.
* **Intent Extras Key:** Use a custom key instead of `R.string.app_user`.
* **Dummy Message:** Customize initial message content or omit step if undesired.

## Edge Cases

| Condition                 | Behavior                                                      |
| :------------------------ | :------------------------------------------------------------ |
| User never chatted before | The conversation appears after the first message is sent.     |
| Target user blocked       | Composer hidden; consider showing unblock prompt.             |
| Invalid user data         | Guard against `null` JSON and show error UI or fallback.      |
| Missing JSON extra        | Do not initialize chat; optionally close screen with warning. |

## Summary / Feature Matrix

| Feature             | Component / Method                                 |
| :------------------ | :------------------------------------------------- |
| Launch private chat | Intent from `messagePrivatelyBtn`                  |
| Initialize chat UI  | `messageHeader.setUser()`, `messageList.setUser()` |
| Create conversation | `CometChat.sendMessage()` optional                 |

<CardGroup>
  <Card title="Android Sample App (Java)">
    Explore this feature in the CometChat SampleApp:
    [GitHub → SampleApp](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-java)
  </Card>

  <Card title="Android Sample App (Kotlin)">
    Explore this feature in the CometChat SampleApp:
    [GitHub → SampleApp](https://github.com/cometchat/cometchat-uikit-android/tree/v5/sample-app-kotlin)
  </Card>
</CardGroup>
