Skip to main content
Version: v4

Users

Overview

The Users is a Component, showcasing an accessible list of all available users. It provides an integral search functionality, allowing you to locate any specific user swiftly and easily. For each user listed, the widget displays the user's name by default, in conjunction with their avatar when available. Furthermore, it includes a status indicator, visually informing you whether a user is currently online or offline.

Image

The Users component is composed of the following BaseComponents:

ComponentsDescription
ListBasea reusable container component having title, search box, customisable background and a List View
ListItema component that renders data obtained from a User object on a Tile having a title, subtitle, leading and trailing view

Usage

Integration

The following code snippet illustrates how you can directly incorporate the Users component into your layout.xml file.

<com.cometchat.chatuikit.users.CometChatUsers
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/users"
/>

Actions

Actions dictate how a component functions. They are divided into two types: Predefined and User-defined. You can override either type, allowing you to tailor the behavior of the component to fit your specific needs.

1. OnSelection

The OnSelection event is activated when you select the done icon in the Menubar while in selection mode. This returns a list of all the users that you have selected.

This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

cometChatUsers.setOnSelection(userList -> {
// your custom action
});
2. ItemClickListener

The OnItemClickListener event is activated when you click on the UserList item. This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

users.setItemClickListener(new OnItemClickListener<User>() {
@Override
public void OnItemClick(User user, int position) {
// your custom action
}
});
3. BackPressListener

The OnBackPressListener function is built to respond when you tap the back button in the Toolbar of the activity.

By default, this action has a predefined behavior: it simply closes the current activity. However, the flexibility of CometChat UI Kit allows you to override this standard behavior according to your application's specific requirements. You can define a custom action that will be performed instead when the back button is pressed.

users.addOnBackPressListener(() -> {
// your custom action
});
4. onError

This action doesn't change the behavior of the component but rather listens for any errors that occur in the User component.

users.setOnError(new OnError() {
@Override
public void onError(Context context, CometChatException e) {
//Your Exception Handling code.
}
});

Filters

Filters allow you to customize the data displayed in a list within a Component. You can filter the list based on your specific criteria, allowing for a more customized. Filters can be applied using RequestBuilders of ChatSDK.

1. UserRequestBuilder

The UserRequestBuilder enables you to filter and customize the user list based on available parameters in UserRequestBuilder. This feature allows you to create more specific and targeted queries when fetching users. The following are the parameters available in UserRequestBuilder

MethodsTypeDescription
setLimitintsets the number users that can be fetched in a single request, suitable for pagination
setSearchKeywordStringused for fetching users matching the passed string
hideBlockedUsersboolused for fetching all those users who are not blocked by the logged in user
friendsOnlyboolused for fetching only those users in which logged in user is a member
setRolesList<String>used for fetching users containing the passed tags
setTagsList<String>used for fetching users containing the passed tags
withTagsboolused for fetching users containing tags
setUserStatusStringused for fetching users by their status online or offline
setUIDsList<String>used for fetching users containing the passed users

Example

In the example below, we are applying a filter to the UserList based on Friends.

UsersRequest.UsersRequestBuilder builder=new UsersRequest.UsersRequestBuilder()
.friendsOnly(false)
.setLimit(10);
cometChatUsers.setUsersRequestBuilder(builder);
2. SearchRequestBuilder

The SearchRequestBuilder uses UserRequestBuilder enables you to filter and customize the search list based on available parameters in UserRequestBuilder. This feature allows you to keep uniformity between the displayed UserList and searched UserList.

Example

UsersRequest.UsersRequestBuilder builder = new UsersRequest.UsersRequestBuilder()
.setSearchKeyword("**");
users.setSearchRequestBuilder(builder);

Events

Events are emitted by a Component. By using event you can extend existing functionality. Being global events, they can be applied in Multiple Locations and are capable of being Added or Removed.

To handle events supported by Users you have to add corresponding listeners by using CometChatUserEvents

EventsDescription
ccUserBlockedThis will get triggered when the logged in user blocks another user
ccUserUnblockedThis will get triggered when the logged in user unblocks another user
CometChatUserEvents.addUserListener("LISTENER_TAG", new CometChatUserEvents() {
@Override
public void ccUserBlocked(User user) {
super.ccUserBlocked(user);
}

@Override
public void ccUserUnblocked(User user) {
super.ccUserUnblocked(user);
}
});

Customization

To fit your app's design requirements, you can customize the appearance of the conversation component. We provide exposed methods that allow you to modify the experience and behavior according to your specific needs.

Style

Using Style you can customize the look and feel of the component in your app, These parameters typically control elements such as the color, size, shape, and fonts used within the component.

1. Users Style

You can set the UsersStyle to the Users Component to customize the styling.

UsersStyle usersStyle = new UsersStyle();
usersStyle.setBackground(Color.BLACK);
usersStyle.setCornerRadius(25f);
users.setStyle(usersStyle);

List of properties exposed by MessageListStyle

PropertyDescriptionCode
BackgroundUsed to set the background colorsetBackground(@ColorInt int)
BorderWidthUsed to set border widthsetBorderWidth(int)
BorderColorUsed to set border colorsetBorderColor(@ColorInt int)
CornerRadiusUsed to set border radiussetCornerRadius(float)
BackgroundUsed to set background DrawablesetBackground(Drawable)
TitleAppearanceUsed to customise the appearance of the title in the app barsetTitleAppearance(@StyleRes int)
BackIconTintUsed to set the color of the back icon in the app barsetBackIconTint(@ColorInt int)
SearchBackgroundUsed to set the background color of the search boxsetSearchBackground(@ColorInt int)
SearchBorderRadiusUsed to set the border radius of the search boxsetSearchBorderRadius(int)
SearchIconTintUsed to set the color of the search icon in the search boxsetSearchIconTint(@ColorInt int)
SearchBorderWidthUsed to set the border width of the search boxsetSearchBorderWidth(int)
SearchTextAppearanceUsed to set the style of the text in the search boxsetSearchTextAppearance(@StyleRes int)
LoadingIconTintUsed to set the color of the icon shown while the list of group members is being fetchedsetLoadingIconTint(@ColorInt int)
EmptyTextAppearanceUsed to set the style of the response text shown when fetchig the list of group members has returned an empty listsetEmptyTextAppearance(@StyleRes int)
ErrorTextAppearanceUsed to set the style of the response text shown in case some error occurs while fetching the list of group memberssetErrorTextAppearance(@StyleRes int)
OnlineStatusColorUsed to set the color of the status indicator shown if a group member is onlinesetOnlineStatusColor(@ColorInt int)
SeparatorColorUsed to set the color of the divider separating the group member itemssetSeparatorColor(@ColorInt int)
SectionHeaderTextAppearanceUsed to customise the appearance of the section header text.setSectionHeaderTextAppearance(@StyleRes int)
SectionHeaderTextColorUsed to set the color of the section header text.setSectionHeaderTextColor(@ColorInt int)
2. Avatar Style

To apply customized styles to the Avatar component in the Users Component, you can use the following code snippet. For further insights on Avatar Styles refer

AvatarStyle avatarStyle = new AvatarStyle();
avatarStyle.setBorderWidth(10);
avatarStyle.setBorderColor(Color.BLACK);
users.setAvatarStyle(avatarStyle);
3. StatusIndicator Style

To apply customized styles to the Status Indicator component in the Users Component, You can use the following code snippet. For further insights on Status Indicator Styles refer

StatusIndicatorStyle statusIndicatorStyle = new StatusIndicatorStyle();
statusIndicatorStyle.setCornerRadius(3.5f);
statusIndicatorStyle.setBorderColor(Color.GREEN);
users.setStatusIndicatorStyle(statusIndicatorStyle);

Functionality

These are a set of small functional customizations that allow you to fine-tune the overall experience of the component. With these, you can change text, set custom icons, and toggle the visibility of UI elements.

CometChatUsers users = findViewById(R.id.users);
users.setTitle("Contacts");
users.showBackButton(true);
users.backIconTint(getResources().getColor(R.color.black));
Image

Below is a list of customizations along with corresponding code snippets

PropertyDescriptionCode
Set TitleUsed to set title in the app bar.setTitle("Your custom title")
Set SearchPlaceholderTextUsed to set search placeholder text.setSearchPlaceholderText("Your custom Text")
BackIconUsed to set back button icon.backIcon(@DrawableRes int res)
Show BackButtonUsed to toggle visibility for back button.showBackButton(boolean)
Set SearchBoxIconUsed to set search Icon in the search field.setSearchBoxIcon(@DrawableRes int res)
Hide SearchUsed to toggle visibility for search box.hideSearch(boolean)
Hide ErrorUsed to hide error on fetching users.hideError(boolean)
Hide SeparatorUsed to hide the divider separating the user items.hideSeparator(boolean)
Hide SectionSeparatorUsed to hide the text separating grouped user items.hideSephideSectionSeparatorarator(boolean)
Disable UsersPresenceUsed to control visibility of user indicator shown if user is online.disableUsersPresence(boolean)
Set SelectionIconUsed to override the default selection complete icon.setSelectionIcon(@DrawableRes int res)
Set SubmitIconUsed to override the default selection complete icon.setSubmitIcon(@DrawableRes int res)
EmptyState TextUsed to set a custom text response when fetching the users has returned an empty list.emptyStateText("Your Text")
ErrorState TextUsed to set a custom text response when some error occurs on fetching the list of users.errorStateText("Your Text")

Advanced

For advanced-level customization, you can set custom views to the component. This lets you tailor each aspect of the component to fit your exact needs and application aesthetics. You can create and define your views, layouts, and UI elements and then incorporate those into the component.


SetListItemView

With this function, you can assign a custom ListItem to the Conversations Component.

users.setListItemView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
return null;
}

@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
}
});

Example

Image

You can indeed create a custom layout file named item_conversation_list.xml for more complex or unique list items.

Once this layout file is made, you would inflate it inside the createView() method of the UsersViewHolderListener. The inflation process prepares the layout for use in your application:

Following this, you would use the bindView() method to initialize and assign values to your individual views. This could include setting text on TextViews, images on ImageViews, and so on based on the properties of the User object:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
app:cardBackgroundColor="@color/purple_500"
app:cardCornerRadius="10dp"
android:layout_margin="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.cometchat.chatuikit.shared.views.CometChatAvatar.CometChatAvatar
android:id="@+id/item_avatar"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
/>
<TextView
android:id="@+id/txt_item_name"
android:text="name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/item_avatar"
android:textSize="17sp"
/>
<TextView
android:id="@+id/txt_item_date"
android:text="date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:textSize="12sp"
android:layout_margin="10dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
YourActivity.java
users.setListItemView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
View view = getLayoutInflater().inflate(R.layout.item_converation_list, null);
return view;
}
@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
CometChatAvatar avatarView = view.findViewById(R.id.item_avatar);
avatarView.setRadius(100);
TextView nameView = view.findViewById(R.id.txt_item_name);
nameView.setText(user.getName());
avatarView.setImage(user.getAvatar(),user.getName());
}
});

SetSubTitleView

You can customize the subtitle view for each conversation item to meet your requirements

users.setSubTitleView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
return null;
}
@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
}
});

Example

Image

You can indeed create a custom layout file named subtitle_layout.xml for more complex or unique list items.

Once this layout file is made, you would inflate it inside the createView() method of the UsersViewHolderListener. The inflation process prepares the layout for use in your application:

Following this, you would use the bindView() method to initialize and assign values to your individual views. This could include setting text on TextViews, images on ImageViews, and so on based on the properties of the User object:

subtitle_layout.xml
    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/txt_subtitle"
android:text="Subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/img_conversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_message_grey"
android:layout_margin="2dp"
android:layout_below="@+id/txt_subtitle"
/>
<ImageView
android:id="@+id/img_audio_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_call"
android:layout_below="@+id/txt_subtitle"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/img_conversation"
/>
<ImageView
android:id="@+id/img_video_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_video"
android:layout_below="@+id/txt_subtitle"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/img_audio_call"
/>

</RelativeLayout>
users.setSubTitleView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
View view = getLayoutInflater().inflate(R.layout.subtitle_layout, null);
return view;
}
@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
TextView txtSubtitle = view.findViewById(R.id.txt_subtitle);
ImageView imgConversation = view.findViewById(R.id.img_conversation);
ImageView imgAudioCall = view.findViewById(R.id.img_audio_call);;
ImageView imgVideCall = view.findViewById(R.id.img_video_call);;
txtSubtitle.setText(user.getName());
imgConversation.setOnClickListener(v -> {
Toast.makeText(context, "Conversation Clicked", Toast.LENGTH_SHORT).show();
});
imgAudioCall.setOnClickListener(v -> {
Toast.makeText(context, "Audio Call Clicked", Toast.LENGTH_SHORT).show();
});
imgVideCall.setOnClickListener(v -> {
Toast.makeText(context, "Video Call Clicked", Toast.LENGTH_SHORT).show();
});
}
});

SetTailView

Used to generate a custom trailing view for the UserList item. You can add a Tail view using the following method.

users.setTailView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
return null;
}
@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
}
});

Example

Image

You can indeed create a custom layout file named tail_view_layout.xml for more complex or unique list items.

Once this layout file is made, you would inflate it inside the createView() method of the UsersViewHolderListener. The inflation process prepares the layout for use in your application:

Following this, you would use the bindView() method to initialize and assign values to your individual views. This could include setting text on TextViews, images on ImageViews, and so on based on the properties of the User object:

tail_view_layout.xml
    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/txt_subtitle"
android:text="Subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/img_conversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_message_grey"
android:layout_margin="2dp"
android:layout_below="@+id/txt_subtitle"
/>
<ImageView
android:id="@+id/img_audio_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_call"
android:layout_below="@+id/txt_subtitle"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/img_conversation"
/>
<ImageView
android:id="@+id/img_video_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_video"
android:layout_below="@+id/txt_subtitle"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/img_audio_call"
/>

</RelativeLayout>
users.setTailView(new UsersViewHolderListener() {
@Override
public View createView(Context context, CometChatListItem cometChatListItem) {
View view = getLayoutInflater().inflate(R.layout.subtitle_layout, null);
return view;
}
@Override
public void bindView(Context context, View view, User user, RecyclerView.ViewHolder viewHolder, List<User> list, int i) {
TextView txtSubtitle = view.findViewById(R.id.txt_subtitle);
ImageView imgConversation = view.findViewById(R.id.img_conversation);
ImageView imgAudioCall = view.findViewById(R.id.img_audio_call);;
ImageView imgVideCall = view.findViewById(R.id.img_video_call);;
txtSubtitle.setText(user.getName());
imgConversation.setOnClickListener(v -> {
Toast.makeText(context, "Conversation Clicked", Toast.LENGTH_SHORT).show();
});
imgAudioCall.setOnClickListener(v -> {
Toast.makeText(context, "Audio Call Clicked", Toast.LENGTH_SHORT).show();
});
imgVideCall.setOnClickListener(v -> {
Toast.makeText(context, "Video Call Clicked", Toast.LENGTH_SHORT).show();
});
}
});

SetMenu

You can set the Custom Menu view to add more options to the Users component.

users.setMenu(View v);

Example

Image

You need to create a view_menu.xml as a custom view file. Which we will inflate and pass to .setMenu().

view_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<ImageView
android:id="@+id/img_refresh"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_refresh_black"
/>
</LinearLayout>

You inflate the view and pass it to setMenu. You can get the child view reference and can handle click actions.


View view = getLayoutInflater().inflate(R.layout.view_menu, null);
ImageView imgRefresh = view.findViewById(R.id.img_refresh);
imgRefresh.setOnClickListener(v -> {
Toast.makeText(this, "Clicked on Refresh", Toast.LENGTH_SHORT).show();
});
users.setMenu(view);


SetLoadingStateView

You can set a custom loader view using setLoadingStateView to match the loading view of your app.

users.setLoadingStateView();

Example

Image

We have added a ContentLoadingProgressBar to loading_view_layout.xml. You can choose any view you prefer. This view should be inflated and passed to the setLoadingStateView() method.

loading_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
>
<androidx.core.widget.ContentLoadingProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:layout_gravity="center_horizontal"
/>
</FrameLayout>
YourActivity.java
users.setLoadingStateView(R.layout.loading_view_layout);

SetEmptyStateView

You can set a custom EmptyStateView using setEmptyStateView to match the empty view of your app.

users.setEmptyStateView();

Examples

Image

We have added an error view to empty_view_layout.xml. You can choose any view you prefer. This view should be inflated and passed to the setEmptyStateView() method.

empty_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
>

<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Friends available"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/cometchat_grey"
/>

</RelativeLayout>
YourActivity.java
users.setEmptyStateView(R.layout.empty_view_layout);

SetErrorStateView

You can set a custom ErrorStateView using setErrorStateView to match the error view of your app.

users.setErrorStateView();

Example

Image

We have added an error view to error_state_view_layout.xml. You can choose any view you prefer. This view should be inflated and passed to the setErrorStateView() method.

error_state_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/img_error"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_error"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something Went Wrong"
android:textSize="30sp"
android:layout_below="@+id/img_error"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
/>

</RelativeLayout>
YourActivity.java
users.setErrorStateView(R.layout.error_state_view_layout);