setOnStartActionModeCallbacks

@RequiresApi(value = 23)
fun setOnStartActionModeCallbacks(onPrepareActionMode: (actionMode: ActionMode, type: Int) -> Unit? = null, onActionItemClicked: (mode: ActionMode, MenuItem) -> Boolean? = null)

You use this method to set onPrepareActionMode that will be called when the user selects text in the WebView. You can also provide onActionItemClicked listener to act when your added items are clicked.

You can use these callbacks to manipulate the context menu as follows:

// To set the callback:
colibrioReadingSystemView.setOnStartActionModeCallbacks(
onPrepareActionMode = { actionMode, type ->
// To clear the menu call:
actionMode.menu.clear()

// To add an item to the menu, you can call one of the menu's add methods, then set a click listener. For example:
actionMode.menu.add("Annotate").setOnMenuItemClickListener {
// Do something

// If you want the menu to be dismissed when the user clicks the item, call:
actionMode.finish()

// Return true to indicate that this click has been handled
return@setOnMenuItemClickListener true
}

// To remove a specific item from the menu call:
actionMode.menu.removeItem(menuItemId)

// To add multiple items from a predefined menu file, use the following command,
// and use the onItemClick listener to act when an item is clicked
actionMode.menuInflater.inflate(R.menu.xyz, actionMode.menu)

return@setOnStartActionModeCallbacks actionMode
},
onActionItemClicked = { actionMode, menuItem ->
val handled = when (menuItem.itemId) {
R.id.action_1 -> {
// Do something when action 1 is clicked
true
}
R.id.action_2 -> {
// Do something when action 2 is clicked
true
}
else -> false // This allows the WebView to act when the default items are clicked
}

if (handled) {
// If you want the menu to be dismissed when the user clicks the item, call:
actionMode.finish()
}

return@setOnStartActionModeCallbacks handled
}
)

// To remove the callback:
colibrioReadingSystemView.setOnStartActionModeCallbacks(null, null)