Enable users to block and unblock others directly within chat using CometChat’s Android UI Kit v5+, preventing unwanted communication and giving users more control.

Overview

Blocking a user stops them from sending messages to the blocker. The CometChat UIKit handles most behaviors internally:
  • Composer Hidden: The message composer is hidden when chatting with a blocked user.
  • Unblock Prompt: An “Unblock” button is displayed to reverse the block.
  • Message Restrictions: Blocked users cannot send messages to the blocker.

Prerequisites

  • Android Studio project with CometChat Android UI Kit v5 added to build.gradle.
  • CometChat App ID, Auth Key, and Region configured and initialized.
  • <uses-permission android:name="android.permission.INTERNET"/> in AndroidManifest.xml.
  • Logged-in user via CometChat.login().
  • Existing one-on-one chat screen using CometChatMessageList and CometChatMessageComposer.

Components

Component / ClassRole
UserDetailActivity.javaDisplays user profile and provides block/unblock options.
MessagesActivity.javaHosts the chat screen and toggles UI based on block state.
CometChatUIKit.blockUsers()API to block one or more users by UID.
CometChatUIKit.unblockUsers()API to unblock one or more users by UID.
User.isBlockedByMe()Checks if the current user has blocked this user.
unblockLayout (View)Layout shown when a user is blocked, containing unblock.
CometChatMessageComposerHidden when chatting with a blocked user.

Integration Steps

1. Detect Block Status

Update UI when block state changes.
// In MessagesActivity.java
private void updateUserBlockStatus(User user) {
    boolean blocked = user.isBlockedByMe();
    binding.messageComposer.setVisibility(blocked ? View.GONE : View.VISIBLE);
    binding.unblockLayout.setVisibility(blocked ? View.VISIBLE : View.GONE);
}
File reference:
MessagesActivity.java
Ensures the composer and unblock UI reflect the current block state.

2. Hide Composer & Show Unblock UI

Define layout elements and their visibility toggles.
<!-- In activity_messages.xml -->
<com.cometchat.ui_kits.CometChatMessageComposer
    android:id="@+id/messageComposer"
    ... />
<View
    android:id="@+id/unblockLayout"
    android:visibility="gone"
    ...>
    <Button
        android:id="@+id/unblockBtn"
        android:text="Unblock"
        ... />
</View>
File reference:
activity_messages.xml
Prepares the UI containers for dynamic show/hide operations.

3. Trigger Unblock Action

Call the unblock API and observe status updates.
// In MessagesActivity.java
binding.unblockBtn.setOnClickListener(v -> viewModel.unblockUser());

// In MessagesViewModel.java
public void unblockUser() {
    CometChatUIKit.unblockUsers(Collections.singletonList(currentUser.getUid()), new CometChat.CallbackListener<List<String>>() {
        @Override
        public void onSuccess(List<String> uids) {
            unblockButtonState.postValue(false);
        }
        @Override
        public void onError(CometChatException e) {
            // Handle error
        }
    });
}
File references: Executes unblock logic and updates LiveData to refresh UI.

4. Trigger Block from Detail Screen

Allow blocking directly from a user’s profile.
// In UserDetailActivity.java
binding.blockMenuItem.setOnClickListener(v -> {
    CometChatUIKit.blockUsers(Collections.singletonList(user.getUid()), new CometChat.CallbackListener<List<String>>() {
        @Override
        public void onSuccess(List<String> uids) {
            viewModel.refreshUser();
        }
        @Override
        public void onError(CometChatException e) {
            // Handle error
        }
    });
});
File reference:
UserDetailActivity.java
Integrates block action into the profile menu, triggering the UI toggle via LiveData.

Implementation Flow

StepActionLocation
1Check block statusupdateUserBlockStatus() in MessagesActivity.java fileciteturn9file0
2Show/hide composer and unblock layoutactivity_messages.xml fileciteturn9file0
3Unblock API call and LiveData updateMessagesViewModel.unblockUser()
4Block API call from profileUserDetailActivity.blockMenuItem

Customization Options

  • Feedback UI: Show a Toast or Snackbar upon success/failure.
  • Menu Icons/Text: Toggle icon and text dynamically after block/unblock.
  • Disable History: Optionally gray out or hide past messages when blocked.

Filtering & Edge Cases

CaseBehavior
Blocked UserComposer hidden; unblock layout shown.
Block SelfSDK ignores request; ensure menu is disabled.
Group ChatBlocking affects only 1:1 communication.
Network FailureObserve error callbacks and retry as needed.

Error Handling

  • Handle errors from blockUsers and unblockUsers callbacks by showing alerts or retry prompts.
  • Ensure UI LiveData states revert on failure.

Group vs. User-Level Differences

ScenarioUI Behavior
ReceiverType.USERBlock/unblock enabled.
ReceiverType.GROUPBlock option ignored; chat continues.

Summary / Feature Matrix

FeatureComponent / Method
Check block stateUser.isBlockedByMe()
Block userCometChatUIKit.blockUsers()
Unblock userCometChatUIKit.unblockUsers()
Update UIupdateUserBlockStatus()
Profile block menuUserDetailActivity.blockMenuItem

Next Steps & Further Reading

Android Sample App (Java)

Explore this feature in the CometChat SampleApp: GitHub → SampleApp

Android Sample App (Kotlin)

Explore this feature in the CometChat SampleApp: GitHub → SampleApp