Maui for Android editor control issue

Haviv Elbsz 2,071 Reputation points
2024-03-22T10:57:08.9833333+00:00

Hi All. VS Ver 17.10 preview 1 and android Ver 34. Currently I have Cut Copy Paste in the Editor context menu. How I can add Undo and Delete to that Manu context. Thank you.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,482 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,351 Reputation points Microsoft Vendor
    2024-03-26T05:53:36.5366667+00:00

    Hello,

    If possible to add to it an 'undo' menu item.

    Yes, you can create a EditorHandler and add CustomSelectionActionModeCallback

            Microsoft.Maui.Handlers.EditorHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
                {
    #if ANDROID
                    handler.PlatformView.CustomSelectionActionModeCallback = new MyCustomSelectionActionModelCallBack();
    
    
    #endif
                });
    

    Next, please create class called MyCustomSelectionActionModelCallBack.cs. Then you can add menu in the OnCreateActionMode and handle the undo menu click event in the OnActionItemClicked method.

    #if ANDROID
        internal class MyCustomSelectionActionModelCallBack : Java.Lang.Object, ActionMode.ICallback
        {
            public bool OnActionItemClicked(ActionMode? mode, IMenuItem? item)
            {
                if(item.ItemId ==10086)
                {
                    //handle your undo operation
                }
                return false;
            }
    
           public bool OnCreateActionMode(ActionMode? mode, IMenu? menu)
            {
                //add undo Menu
                menu.Add(0, 10086, 0, "Undo");
    
    
               return true;
            }
    
           public void OnDestroyActionMode(ActionMode? mode)
            {
               
            }
            public bool OnPrepareActionMode(ActionMode? mode, IMenu? menu)
            {
               
                return false;
            }
        }
    #endif
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.