Building Custom Message List Items With Compose

5 min read

Creating custom message list items is a common requirement for many developers using Stream’s new Compose Chat SDK. In this post, you’ll learn just how easy it is to build your own custom message list items and implement them in your app.

Márton B.
Márton B.
Published December 8, 2021
Custom message list items feature image

The Stream Chat SDK for Jetpack Compose makes extensive use of slot APIs, which allow you to provide pieces of Composable layout that you can then use within one of the chat components provided by the SDK.

For this post, you'll use the MessageList component and its itemContentparameter to completely change how message items are rendered within a message list while keeping all the powerful built-in behavior, like paging and scrolling.

By the end, you'll build the following three custom looks for the items:

  • Colorful message bubbles
  • Livestream messages
  • A team messaging app
Message list items customizations

The itemContent parameter

A prime example of Stream's slot APIs is the MessageList component, which displays a list of messages:

Message list component

Within this component, the itemContent composable parameter is what renders each message in the list:

kotlin
MessageList(
    modifier = Modifier.fillMaxSize(),
    viewModel = listViewModel,
    itemContent = { item ->
        CustomMessageItemContent(item) // <- Your custom composable function
    },
)

This is what you'll customize in various ways, by providing our own itemContent components. You can try all of these implementations in the GitHub repository for this tutorial.

Note: We'll only cover the visual customization of the items here and skip attaching things like click listeners within the custom items, which is something you'd want to add in your real code.

Building Colorful Message Bubbles

Starting off with a look that's similar to the default, we'll build this design:

Customized message list bubbles

In this example, we're removing elements like the footer (which provides information like the user's name and timestamps) and reusing the Chat SDK's avatar and bubble components.

And of course, we're assigning a random color to each user's messages.

Let's see the code, with the important bits explained in the comments:

kotlin
// Helper function to display a user avatar or a spacer
@Composable
private fun RowScope.MessageAvatar(
    position: MessageItemGroupPosition,
    user: User,
) {
    if (position == Bottom || position == None) {
        val isCurrentUser = user.id == ChatClient.instance().getCurrentUser()?.id
        // Provided by the SDK to display a User object's avatar
        UserAvatar(
            modifier = Modifier
                .padding(start = 8.dp, end = 8.dp)
                .size(24.dp)
                .clip(CircleShape)
                .background(Color.LightGray)

As you can see, lots of this UI is built from basic Compose constructs, like Rows, Columns, Spacers, and Text.

However, smaller components from the SDK are also reused so that you don't have to construct everything from scratch. Components like UserAvatar or MessageAttachmentsContent really come in handy when you build custom layouts like these.

Building your own app? Get early access to our Livestream or Video Calling API and launch in days!

Building Livestream Messages

Our next design will be very basic, something you might add to a live-streaming screen within an application:

Customized livestream chat

This example won't support any attachments or fancy features. Instead, we will focus on displaying the user's messages in a compact UI.

This approach is ideal for handling real-time events where messages accumulate quickly.

Now, to the code:

kotlin
@Composable
fun LiveStreamItemContent(messageItem: MessageItem) {
    val message = messageItem.message
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 2.dp, horizontal = 4.dp)
            .clip(RoundedCornerShape(0.dp))
            // Semi-transparent background for the live chat
            .background(Color(0x443474EB))
            .padding(4.dp)
            .widthIn(max = 300.dp)
    ) {
        Row {
            UserAvatar(

There's not much to this one, as the design is super simple:

We reuse UserAvatar to build a string from the username and message.

This way, they can be styled and displayed within a single Text component, allowing them to flow nicely between lines.

Building Team Messaging Items

For the final example, let's look at something you might use for a team messaging app.

Customized team messaging items

As you can see, all the messages are left-aligned, and we prominently display the avatar and name of the user who sent the message.

This is useful in a corporate environment, where users won't recognize everyone just by their avatar (like they would in a small friend group).

The code for this custom layout is the following:

kotlin
@Composable
fun TeamChatItemContent(messageItem: MessageItem) {
    val message = messageItem.message
    val firstItem = messageItem.groupPosition == Top || messageItem.groupPosition == None

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp)
            .widthIn(max = 300.dp)
    ) {
        if (firstItem) {
            Spacer(Modifier.padding(4.dp))
        }

Again, this layout builds mostly on basic Compose constructs, and most of the logic deals with setting correct paddings between the elements.

One highlight is the Timestamp component from the SDK, which you can use to quickly format a Date object into a date or time format.

Conclusion

As a quick reminder, you can check out all of the code and see these layouts in action by checking out the GitHub repository for this tutorial.

If you want to try the SDK and see how easy it is to get started, take a look at the Compose In-App Messaging Tutorial. You can also take a look at the Compose UI Components documentation which describes all the available components, features, and customization options.

You'll also find all the Compose SDK source code in the Stream Chat Android GitHub repository. We'd love to hear your feedback there, either in the form of issues or discussions.

decorative lines
Integrating Video With Your App?
We've built an audio and video solution just for you. Launch in days with our new APIs & SDKs!
Check out the BETA!