/ UIKit / Android View
UIKit
Chat UIKit Android View v3
Chat UIKit Android View
Chat UIKit
Android View
Version 3

Add a new message type

Copy link

You can create new message types other than the ones provided by Sendbird UIKit.


Sending messages with custom data

Copy link

First, define the custom message type. Sendbird UIKit reserves message types from 0 to 1000. You should define custom message types from 1001 as shown in the sample code below.

const val NEW_MESSAGE_TYPE = 1001
const val NEW_FILE_MESSAGE_TYPE = 1002

Before sending a message, you can set its customType. You can also include data that will be used to render the UI for this custom message.

SendbirdUIKit.setCustomParamsHandler(object : CustomParamsHandler {
    override fun onBeforeSendUserMessage(params: UserMessageCreateParams) {
        params.customType = NEW_MESSAGE_TYPE.toString()
        params.data = "DATA FOR CUSTOM UI"
    }
    override fun onBeforeSendFileMessage(params: FileMessageCreateParams) {
        params.customType = NEW_FILE_MESSAGE_TYPE.toString()
        params.data = "DATA FOR CUSTOM UI"
    }
})

Drawing new message types

Copy link

Rendering a new message type is similar to declaring a viewType in an Android RecyclerView. First, you inherit from MessageListAdapter and implement the getItemViewType() method. Based on the custom type returned from getItemViewType(), you can render a custom ViewHolder. Follow the sample code below.

private const val NEW_MESSAGE_TYPE = 1001
private const val NEW_FILE_MESSAGE_TYPE = 1002

class NewMessageTypeSampleAdapter(
    channel: GroupChannel?,
    uiParams: MessageListUIParams
) : MessageListAdapter(channel, uiParams) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            NEW_MESSAGE_TYPE -> {
                return NewMessageTypeSampleViewHolder(
                    ViewCustomMessageBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
            NEW_FILE_MESSAGE_TYPE -> {
                return NewFileTypeMessageViewHolder(
                    ViewCustomFileMessageBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun getItemViewType(position: Int): Int = with(getItem(position)) {
        return when (this.customType) {
            NEW_MESSAGE_TYPE.toString() -> return NEW_MESSAGE_TYPE
            NEW_FILE_MESSAGE_TYPE.toString() -> return NEW_FILE_MESSAGE_TYPE
            else -> MessageViewHolderFactory.getViewType(this)
        }
    }

    class NewMessageTypeSampleViewHolder(
        val binding: ViewCustomMessageMeBinding
    ) : GroupChannelMessageViewHolder(binding.root) {
        override fun setEmojiReaction(
            reactionList: MutableList<Reaction>,
            emojiReactionClickListener: OnItemClickListener<String>?,
            emojiReactionLongClickListener: OnItemLongClickListener<String>?,
            moreButtonClickListener: View.OnClickListener?
        ) {}

        override fun bind(channel: BaseChannel, message: BaseMessage, params: MessageListUIParams) {
            // Draw your custom message view
        }

        override fun getClickableViewMap(): Map<String, View> = mapOf()
    }
}

For an in-depth practical demonstration, see our sample code.