androidxamarinxamarin.formsmiuiredmi-device

How to Disable copy/paste option of Xamarin forms Entry control in Redmi note 12 pro device?


I want to disable copy/paste option of my Entry control in Xamarin forms application. I am using custom renderer for that. The current solution is working in all the other devices apart from Redmi Note 8. This is my renderer code.

class MyEntryRenderer : EntryRenderer
    {
      
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
              Control.CustomSelectionActionModeCallback = new Callback();
                Control.CustomInsertionActionModeCallback = new Callback();

                Console.WriteLine("CustomSelectionActionModeCallback");
                Control.SetTextIsSelectable(false);
                Control.LongClickable = false;
            }
        }
    }
    public class Callback : Java.Lang.Object, ActionMode.ICallback
        {
            public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
            {
            Console.WriteLine("OnActionItemClicked");
                return true;
            }
            public bool OnCreateActionMode(ActionMode mode, IMenu menu)
            {
            Console.WriteLine("OnCreateActionMode");
            menu.Clear();
            return false;
            }
            public void OnDestroyActionMode(ActionMode mode) {
            Console.WriteLine("OnDestroyActionMode");
        }
            public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
            {
            Console.WriteLine("OnPrepareActionMode");
            menu.Clear();
            menu.Close();
            return true;
            }
        }
    }

So long click is getting disabled in Redmi Note 8 but still there is blue color dot which appears. On click of that it still shows me copy/paste option. It is happening only in Redmi note 8. And in my code no other callback is getting hit other than OnDestroyActionMode and I'm not able to execute menu.Clear. In other devices that option is getting disabled by using this code

Control.SetTextIsSelectable(false);

This is how it is getting shown in Redmi Note 8 device.

enter image description here

I have referred these links because this issue is quite similar to mine but it didn't help.

Disable EditText context menu

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event

As you can see in the image the paste option is getting shown in the device. I have no clue how to fix this bug any suggestions?


Solution

  • I have found two solutions for this. One is clearing the clipboard and other is setting the variation of input as visible password. Both these solutions serves my purpose. For clearing the clipboard you can use the following code in your renderer

    var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
    clipboardManager.ClearPrimaryClip();
    

    And for setting the variation of input as visible password you can use the following code

    Control.InputType = Android.Text.InputTypes.ClassText | Android.Text.InputTypes.TextVariationVisiblePassword;
    

    So these are the solutions which was useful for me.