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

# Retrieve Users

> Fetch user details, retrieve user lists with filters, and get online user counts using the Android SDK

> [accordion] AI Integration Quick Reference
>
> ```kotlin theme={null}
> // Get logged-in user
> val user = CometChat.getLoggedInUser()
>
> // Fetch user list with filters
> val usersRequest = UsersRequestBuilder()
>     .setLimit(30)
>     .setSearchKeyword("john")
>     .hideBlockedUsers(true)
>     .build()
>
> usersRequest.fetchNext(object : CallbackListener<List<User?>>() {
>     override fun onSuccess(list: List<User?>) { }
>     override fun onError(e: CometChatException) { }
> })
>
> // Get specific user details
> CometChat.getUser("UID", object : CometChat.CallbackListener<User>() {
>     override fun onSuccess(user: User) { }
>     override fun onError(e: CometChatException) { }
> })
> ```

## Retrieve Logged In User Details

Use `getLoggedInUser()` to get the details of the logged-in user. Returns `null` if no user is logged in.

> [tab] Java
>
> ```java theme={null}
> User user = CometChat.getLoggedInUser();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val user = CometChat.getLoggedInUser()   
> ```

This method will return a [`User`](https://www.cometchat.com/docs/sdk/reference/entities#user) object containing all the information related to the logged-in user.

## Retrieve List of Users

Use `UsersRequestBuilder` to configure filters, then call `fetchNext()` to retrieve users.

### Set Limit

Set the number of users to fetch per request.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(30)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(30)
> .build()
> ```

### Set Search Keyword

Filter users by a search string.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .setSearchKeyword("abc")
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .setSearchKeyword("abc")
> .build()
> ```

### Search In

Define which user property the `searchKeyword` should be searched in. Works only with `setSearchKeyword()`. By default, searches both UID and Name.

> [tab] Java
>
> ```java theme={null}
> List<String> searchInList = new ArrayList<>();
> searchInList.add("uid");
>
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(20)
> .setSearchKeyword("super")
> .searchIn(searchInList)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val searchInList: MutableList<String> = ArrayList()
> searchInList.add("uid")
>
> val usersRequest = UsersRequestBuilder()
> .setLimit(20)
> .setSearchKeyword("super")
> .searchIn(searchInList)
> .build()
> ```

### Set Status

The status based on which the users are to be fetched. The status parameter can contain one of the following values:

* `CometChat.USER_STATUS.ONLINE` - Returns the list of only online users.
* `CometChat.USER_STATUS.OFFLINE` - Returns the list of only offline users.

If this parameter is not set, all available users will be returned.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .setUserStatus(UsersRequest.USER_STATUS_ONLINE)
> .build();  
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .setUserStatus(UsersRequest.USER_STATUS_ONLINE)
> .build() 
> ```

If this parameter is not set, all users will be returned.

### Hide Blocked Users

Exclude users blocked by the logged-in user from the results. Default is `false`.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .hideBlockedUsers(true)
> .build();   
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .hideBlockedUsers(true)
> .build()
> ```

### Set Roles

Filter users by one or more roles.

> [tab] Java
>
> ```java theme={null}
> List<String> roles = new ArrayList<>();
> roles.add("role1");
> roles.add("role2");
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .setRoles(roles)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val roles: MutableList<String> = ArrayList()
> roles.add("role1")
> roles.add("role2")
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .setRoles(roles)
> .build()
> ```

### Friends Only

Return only friends of the logged-in user when set to `true`.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .friendsOnly(true)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .friendsOnly(true)
> .build()
> ```

### Set Tags

Filter users by tags. Only users tagged with the specified tags are returned.

> [tab] Java
>
> ```java theme={null}
> List<String> tags = new ArrayList<>();
> tags.add("tag1");
> tags.add("tag2");
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .setTags(tags)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val tags: MutableList<String> = ArrayList()
> tags.add("tag1")
> tags.add("tag2")
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .setTags(tags)
> .build()
> ```

### With Tags

Include tag data in the response when set to `true`.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .withTags(true)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .withTags(true)
> .build() 
> ```

### Set UIDs

Fetch specific users by their UIDs. Maximum 25 UIDs per request.

> [tab] Java
>
> ```java theme={null}
> List<String> uids = new ArrayList<>();
> uids.add("cometchat-uid-1");
> uids.add("cometchat-uid-2");
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(limit)
> .setUIDs(uids)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val uids: MutableList<String> = ArrayList()
> uids.add("cometchat-uid-1")
> uids.add("cometchat-uid-2")
> val usersRequest = UsersRequestBuilder()
> .setLimit(limit)
> .setUIDs(uids)
> .build()
> ```

### Sort By

Sort the user list by a specific property. Default sort order is `status => name => UID`.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(20)
> .sortBy(CometChatConstants.SORT_BY_NAME)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(20)
> .sortBy(CometChatConstants.SORT_BY_NAME)
> .build() 
> ```

### Sort By Order

Sort the user list in ascending or descending order. Default is ascending.

> [tab] Java
>
> ```java theme={null}
> UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
> .setLimit(20)
> .sortByOrder(CometChatConstants.SORT_ORDER_DESCENDING)
> .build();
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val usersRequest = UsersRequestBuilder()
> .setLimit(20)
> .sortByOrder(CometChatConstants.SORT_ORDER_DESCENDING)
> .build()
> ```

### Fetch Users

After configuring the builder, call `build()` then `fetchNext()` to retrieve users. Call `fetchNext()` repeatedly on the same object to paginate.

> [tab] Java
>
> ```java theme={null}
> private UsersRequest usersRequest = null;
> private int limit = 30;
>
> usersRequest = new UsersRequest.UsersRequestBuilder().setLimit(limit).build();
>
> usersRequest.fetchNext(new CometChat.CallbackListener<List<User>>() {
> @Override
> public void onSuccess(List <User> list) {
>   Log.d(TAG, "User list received: " + list.size());
> }
> @Override
> public void onError(CometChatException e) {
>   Log.d(TAG, "User list fetching failed with exception: " + e.getMessage());
> }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> var usersRequest: UsersRequest? = null
> val limit = 30
>
> usersRequest = UsersRequestBuilder().setLimit(limit).build()
>
> usersRequest.fetchNext(object : CallbackListener<List<User?>>() {
> override fun onSuccess(list: List<User?>) {
>   Log.d(TAG, "User list received: " + list.size)
> }
>
> override fun onError(e: CometChatException) {
>   Log.d(TAG, "User list fetching failed with exception: " + e.message)
> }
> })
> ```

## Retrieve Particular User Details

Use `getUser()` to fetch details for a specific user by UID.

> [tab] Java
>
> ```java theme={null}
> private String UID = "UID";
>
> CometChat.getUser(UID, new CometChat.CallbackListener<User>() {
> @Override
> public void onSuccess(User user) {
>   Log.d(TAG, "User details fetched for user: " + user.toString());
> }
> @Override
> public void onError(CometChatException e) {
>   Log.d(TAG, "User details fetching failed with exception: " + e.getMessage());
> }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val UID:String="UID"
>
> CometChat.getUser(UID,object :CometChat.CallbackListener<User>(){
> override fun onSuccess(p0: User?) {
>   Log.d(TAG, "User details fetched for user: " + p0?.toString())
> }
>
> override fun onError(p0: CometChatException?) {
>   Log.d(TAG, "User details fetching failed with exception: " + p0?.message)
> }
> })
> ```

The `getUser()` method takes the following parameters:

| Parameter | Description |
| - | - |
| `UID` | The UID of the user for whom the details are to be fetched |

On success, the [`User`](https://www.cometchat.com/docs/sdk/reference/entities#user) object containing the details of the user is returned.

## Get Online User Count

Use `getOnlineUserCount()` to get the total count of online users for your app.

> [tab] Java
>
> ```java theme={null}
> CometChat.getOnlineUserCount(new CometChat.CallbackListener<Integer>() {
> @Override
> public void onSuccess(Integer count) {
>   Log.d(TAG, "Online users : " + count);
> }
>
> @Override
> public void onError(CometChatException e) {
>   Log.d(TAG, "Error : " + e.getMessage());
> }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.getOnlineUserCount(object : CallbackListener<Int>() {
> override fun onSuccess(count: Int) {
>   Log.d(TAG, "Online users : $count")
> }
>
> override fun onError(e: CometChatException) {
>   Log.d(TAG, "Error : " + e.message)
> }
> })
> ```

## User Payload Structure

> [accordion] User Object
>
> The `User` object returned by SDK methods contains the following fields:
>
> | Parameter | Type | Description |
> | - | - | - |
> | `uid` | String | Unique identifier of the user |
> | `name` | String | Display name of the user |
> | `avatar` | String | URL to user's profile picture |
> | `link` | String | URL to user's profile page |
> | `role` | String | User role for role-based access control |
> | `metadata` | JSONObject | Custom data set by developer. Can contain any key-value pairs |
> | `status` | String | User online status. Values: `"online"`, `"offline"` |
> | `statusMessage` | String | Custom status message set by user |
> | `lastActiveAt` | long | Unix timestamp of last activity (milliseconds) |
> | `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user |
> | `blockedByMe` | boolean | Whether the logged-in user has blocked this user |
> | `tags` | Array\<String> | List of tags for user identification and filtering |
> | `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) |
>
> **Sample User Object:**
>
> ```json theme={null}
> {
>   "uid": "user_123",
>   "name": "John Doe",
>   "avatar": "https://example.com/avatar.png",
>   "link": "https://example.com/profile/user_123",
>   "role": "default",
>   "metadata": {
>     "customKey": "customValue",
>     "preferences": {
>       "theme": "dark",
>       "notifications": true
>     }
>   },
>   "status": "online",
>   "statusMessage": "Available",
>   "lastActiveAt": 1699900000000,
>   "hasBlockedMe": false,
>   "blockedByMe": false,
>   "tags": ["premium", "verified"],
>   "deactivatedAt": 0
> }
> ```

## Next Steps

> [card] [User Presence](https://www.cometchat.com/docs/sdk/android/v4/user-presence.md)
>
> Track and subscribe to user online/offline status updates

> [card] [Block Users](https://www.cometchat.com/docs/sdk/android/v4/block-users.md)
>
> Block and unblock users to control interactions

> [card] [User Management](https://www.cometchat.com/docs/sdk/android/v4/user-management.md)
>
> Create, update, and manage user accounts

> [card] [Send Messages](https://www.cometchat.com/docs/sdk/android/v4/send-message.md)
>
> Start sending messages to retrieved users


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