TheGeekZn TheGeekZn - 2 months ago 20
Android Question

Adding to the Selected Text action menu

I've been on this for days now, all I'm trying to do is add a menu item to the selected text menu, and bind an event to it. Im selecting text in a

WebView
.

So far, I am able to add an item to the menu either by inflating it, or directly adding a menu item.

public override void OnActionModeStarted(ActionMode mode)
{
var item = mode.Menu.Add("Custom");
item.SetOnMenuItemClickListener(new ClickListener());

base.OnActionModeStarted(mode);
}


However, the
OnMenuItemClick(IMenuItem)
inside
ClickListener()
never gets hit.

I've tried to overwrite
ActionMode
, all the click callbacks, all the interfaces and classes I could find, but nothing get's called. I can see the actual menu item, but it does nothing.

I've even tried to set the click handler by using

var size = mode.Menu.Size();
var menuItem = mode.Menu.GetItem(size - 1);


which still doesn't work. During debugging, I can see that item get appended. I've looked everywhere, and I have found no answers.

Fiddling with
OnWindowStartingActionMode(ActionMode.ICallback callback)
also seems fruitless.

enter image description here

Answer

I assume, you implemented IMenuItemOnMenuItemClickListener. When you implement your click listener, you have to derive from Java.Lang.Object.

class ClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener
{
    public bool OnMenuItemClick(IMenuItem item)
    {
        return true;
    }
}

See Implementing Java interfaces for more info.

Comments