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

Customize message context menu

Copy link

Message context menu is a menu that appears when a message is long-pressed. While you can entirely change the message menu using the Message click sample, follow this page to modify the menu to work in tandem with the standard message menu.

To customize the message context menu, you need to create a custom fragment.


Create the custom menu

Copy link

The following are steps to creating the message context menu:

  • Step 1: Edit the context menu list by inheriting from the fragment and implementing the makeMessageContextMenu() method.
  • Step 2: Retrieve the default menu list by calling the parent method.
  • Step 3: You can add or remove items from the retrieved menu list.
  • Step 4: The makeMessageContextMenu() method provides the message that was long-pressed as an argument. Using this information, you can construct the necessary menu.

Example: Adding and removing menu option

Copy link

The following example shows an example of removing the Edit option from the default menu and adding a Resend menu option.

Menu items are differentiated using unique keys in the DialogListItem. These keys are defined as string resources in string.xml.

<string name="sb_text_channel_anchor_copy">Copy</string>
<string name="sb_text_channel_anchor_save">Save</string>
<string name="sb_text_channel_anchor_edit">Edit</string>
<string name="sb_text_channel_anchor_delete">Delete</string>
<string name="sb_text_channel_anchor_reply">Reply</string>
<string name="sb_text_channel_anchor_reply_in_thread">Reply in thread</string>
<string name="sb_text_channel_anchor_retry">Retry</string>

To edit the default menu list and add a new menu option, override the makeMessageContextMenu() method.

To handle the action when the resend option is clicked, override the onMessageContextMenuItemClicked() method. In the sample below, it is resending the user message when this option is selected.

class MessageMenuSampleFragment : ChannelFragment() {
    override fun makeMessageContextMenu(message: BaseMessage): MutableList<DialogListItem> {
        return super.makeMessageContextMenu(message).filterTo(mutableListOf()) {
            it.key != com.sendbird.uikit.R.string.sb_text_channel_anchor_edit
        }.also {
            if (message.sendingStatus == SendingStatus.SUCCEEDED) {
                if (message is UserMessage) {
                    it.add(DialogListItem(R.string.text_resend, R.drawable.icon_reply))
                }
            }
        }
    }
    
    override fun onMessageContextMenuItemClicked(message: BaseMessage, view: View, position: Int, item: DialogListItem): Boolean {
        super.onMessageContextMenuItemClicked(message, view, position, item)
        when (item.key) {
            R.string.text_resend -> {
                sendUserMessage(UserMessageCreateParams(message.message))
                return true
            }
        }
        return false
    }
}

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