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

# Retrieve Groups

> Fetch and filter CometChat groups with the Android SDK using request builders, tags, joined groups, and search.

<Accordion title="AI Integration Quick Reference">
  ```kotlin theme={null}
  // Fetch groups with filters
  val groupsRequest = GroupsRequestBuilder()
      .setLimit(30)
      .joinedOnly(true)
      .setSearchKeyWord("search")
      .build()

  groupsRequest.fetchNext(object : CallbackListener<List<Group>>() {
      override fun onSuccess(groups: List<Group>) { }
      override fun onError(e: CometChatException) { }
  })

  // Get specific group details
  CometChat.getGroup("GUID", callback)
  ```
</Accordion>

## Retrieve List of Groups

Use `GroupsRequestBuilder` to configure filters, then call `fetchNext()` to retrieve groups.

### Set Limit

Set the number of groups to fetch per request.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
    .setLimit(limit)
    .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val groupsRequest = GroupsRequestBuilder()
    .setLimit(limit)
    .build()  
    ```
  </Tab>
</Tabs>

### Set Search Keyword

Filter groups by a search string.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
    .setSearchKeyWord("abc")
    .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val groupsRequest = GroupsRequestBuilder()
    .setSearchKeyWord("abc")
    .build()
    ```
  </Tab>
</Tabs>

### Joined Only

Return only groups the logged-in user has joined.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
    .joinedOnly(true)
    .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val groupsRequest = GroupsRequestBuilder()
    .joinedOnly(true)
    .build()  
    ```
  </Tab>
</Tabs>

### Set Tags

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

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    List<String> tags = new ArrayList<>();
    tags.add("tag1");
    tags.add("tag2");
    GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
    .setLimit(limit)
    .setTags(tags)
    .build();  
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val tags: MutableList<String> = ArrayList()
    tags.add("tag1")
    tags.add("tag2")
    val groupsRequest = GroupsRequestBuilder()
    .setLimit(limit)
    .setTags(tags)
    .build()
    ```
  </Tab>
</Tabs>

### With Tags

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

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
    .setLimit(limit)
    .withTags(true)
    .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val groupsRequest = GroupsRequestBuilder()
    .setLimit(limit)
    .withTags(true)
    .build()
    ```
  </Tab>
</Tabs>

### Fetch Groups

After configuring the builder, call `build()` then `fetchNext()` to retrieve groups. Public and password-protected groups are always included. Private groups only appear if the user is a member.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private GroupsRequest groupsRequest = null;
    private int limit = 30;

    groupsRequest = new GroupsRequest.GroupsRequestBuilder().setLimit(limit).build();

    groupsRequest.fetchNext(new CometChat.CallbackListener<List<Group>>() {
    @Override
    public void onSuccess(List <Group> list) {
      Log.d(TAG, "Groups list fetched successfully: " + list.size());
    }
    @Override
    public void onError(CometChatException e) {
      Log.d(TAG, "Groups list fetching failed with exception: " + e.getMessage());
    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val limit = 30
    val groupsRequest = GroupsRequestBuilder().setLimit(limit).build()

    groupsRequest.fetchNext(object : CallbackListener<List<Group?>>() {
    override fun onSuccess(list: List<Group?>) {
      Log.d(TAG, "Groups list fetched successfully: " + list.size)
    }

    override fun onError(e: CometChatException) {
      Log.d(TAG, "Groups list fetching failed with exception: " + e.message)
    }
    })
    ```
  </Tab>
</Tabs>

## Retrieve Particular Group Details

Use `getGroup()` to fetch details for a specific group by GUID.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private String GUID = "GUID";

    CometChat.getGroup(GUID, new CometChat.CallbackListener<Group>() {
    @Override
    public void onSuccess(Group group) {
      Log.d(TAG, "Group details fetched successfully: " + group.toString());        
    }

    @Override
    public void onError(CometChatException e) { 
      Log.d(TAG, "Group details fetching failed with exception: " + e.getMessage());   

    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val GUID : String = "GUID"

    CometChat.getGroup(GUID,object :CometChat.CallbackListener<Group>(){
    override fun onSuccess(p0: Group?) {
      Log.d(TAG, "Group details fetched successfully: " + p0?.toString())
    }
    override fun onError(p0: CometChatException?) {
      Log.d(TAG, "Group details fetching failed with exception: " +p0?.message)
    }
    })
    ```
  </Tab>
</Tabs>

| Parameter | Description                                                  |
| --------- | ------------------------------------------------------------ |
| `GUID`    | The GUID of the group for whom the details are to be fetched |

On success, the [`Group`](/sdk/reference/entities#group) object containing the details of the group is returned.

## Get Online Group Member Count

Use `getOnlineGroupMemberCount()` to get the count of online members for specific groups.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    List<String> guids = new ArrayList<>();
    guids.add("cometchat-guid-1");
    guids.add("cometchat-guid-11");

    CometChat.getOnlineGroupMemberCount(guids, new CometChat.CallbackListener<HashMap<String, Integer>>() {
    @Override
    public void onSuccess(HashMap<String, Integer> stringIntegerHashMap) {
      Log.d(TAG, "Online count fetched successfully " + stringIntegerHashMap.toString());
    }

    @Override
    public void onError(CometChatException e) {
      Log.d(TAG, e.getMessage());
    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val guids: MutableList<String> = ArrayList()
    guids.add("cometchat-guid-1")
    guids.add("cometchat-guid-11")

    CometChat.getOnlineGroupMemberCount(guids, object : CallbackListener<HashMap<String?, Int?>>() {
    override fun onSuccess(stringIntegerHashMap: HashMap<String?, Int?>) {
      Log.d(TAG, "Online count fetched successfully $stringIntegerHashMap")
    }

    override fun onError(e: CometChatException) {
      Log.d(TAG, e.message!!)
    }
    })
    ```
  </Tab>
</Tabs>

This method returns a `Hashmap` with the GUID of the group as the key and the online member count for that group as the value.

## Group Payload Structure

<Accordion title="Group Object">
  The `Group` object returned by SDK methods contains the following fields:

  | Parameter           | Type           | Description                                                                        |
  | ------------------- | -------------- | ---------------------------------------------------------------------------------- |
  | `guid`              | String         | Unique identifier of the group                                                     |
  | `name`              | String         | Display name of the group                                                          |
  | `type`              | String         | Group type. Values: `"public"`, `"private"`, `"password"`                          |
  | `password`          | String         | Password for protected groups (null for public/private groups)                     |
  | `icon`              | String         | URL to group icon image                                                            |
  | `description`       | String         | Description of the group                                                           |
  | `owner`             | String         | UID of the group owner                                                             |
  | `metadata`          | JSONObject     | Custom data set by developer. Can contain any key-value pairs                      |
  | `createdAt`         | long           | Unix timestamp when group was created                                              |
  | `updatedAt`         | long           | Unix timestamp of last group update                                                |
  | `hasJoined`         | boolean        | Whether the logged-in user has joined this group                                   |
  | `joinedAt`          | long           | Unix timestamp when logged-in user joined the group                                |
  | `scope`             | String         | Logged-in user's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` |
  | `membersCount`      | int            | Total number of members in the group                                               |
  | `tags`              | Array\<String> | List of tags for group identification and filtering                                |
  | `isBannedFromGroup` | boolean        | Whether the logged-in user is banned from this group                               |

  **Sample Group Object:**

  ```json theme={null}
  {
    "guid": "group_123",
    "name": "Developers",
    "type": "public",
    "password": null,
    "icon": "https://example.com/icon.png",
    "description": "A group for developers",
    "owner": "user_123",
    "metadata": {
      "category": "tech",
      "isVerified": true
    },
    "createdAt": 1699800000,
    "updatedAt": 1699900000,
    "hasJoined": true,
    "joinedAt": 1699850000,
    "scope": "admin",
    "membersCount": 25,
    "tags": ["official", "support"],
    "isBannedFromGroup": false
  }
  ```
</Accordion>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Create Group" icon="users" href="/sdk/android/create-group">
    Create new groups for your users
  </Card>

  <Card title="Join Group" icon="user-plus" href="/sdk/android/join-group">
    Join groups to participate in conversations
  </Card>

  <Card title="Retrieve Members" icon="user-group" href="/sdk/android/retrieve-group-members">
    Fetch list of group members
  </Card>

  <Card title="Additional Filtering" icon="filter" href="/sdk/android/additional-message-filtering">
    Learn more about advanced filtering options
  </Card>
</CardGroup>
