How To Hide Menu Options For Xamarin.Forms

Introduction

This article demonstrates how to hide the menu options for entry when we long press or long tap on the text. Here, the menu options mean Cut, Copy, Paste ... etc.

Xamarin

In the above picture, the yellow highlighted section represents menu options.

Prerequisites

  • Windows or Mac machine.
  • Based on the operating system, respective version of Visual Studio has to be installed in it.

When this is useful

In the following cases, this post mainly helps you to hide Cut, Copy and Paste options.

  1. We can restrict the user from copy-pasting the text in case of Password and Confirm password.
  2. We can restrict the user from copy-pasting bank account details and similar sensitive information.

How to create Entry in Xamarin forms

The following is the predefined tag for representing Entry in Xamarin.Forms. It's what we call EditText in Android, UITextfield in iOS, and Textfield in HTML Forms. Here, we are having two options to create Entry. One is using XAML and the second one is C#.

XAML

<Entry Text="This is Entry"></Entry>

C#

  1. var entry = new Entry();  
  2. entry.Text = "This is Entry";  

 There are multiple properties available for Entry. Click here to go through those.

How to hide menu options

We can hide Cut, Copy, and Paste ... etc., options by using Custom Renderers concept of Xamarin.Forms.

Please follow the below steps one by one to complete our requirement.

  1. On the Start screen, launch Visual Studio. This opens the start page of Visual Studio like below.

    Xamarin
  1. In Visual Studio, click "Create new project…" (Yellow highlighted section) to create a new project.
    Xamarin
  1. In the New Project dialog, click Cross-Platform, select the Mobile App (Xamarin.Forms) template, set the Name and Solution name to DisableCutCopyPaste, choose a suitable location for the project and click the OK button:

    Xamarin
  1. Create a class with some name (CustomEntry) in PCL/NET Standard project.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using Xamarin.Forms;  
    5.   
    6. namespace DisableCutCopyPaste  
    7. {  
    8.         public class CustomEntry : Entry  
    9.         {  
    10.         }  
    11. }  
  1. Creating the custom renderer for Android platform as shown below.
    1. using Android.Content;  
    2. using Android.Views;  
    3. using DisableCutCopyPaste;  
    4. using DisableCutCopyPaste.Droid;  
    5. using Xamarin.Forms;  
    6. using Xamarin.Forms.Platform.Android;  
    7.   
    8. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
    9. namespace DisableCutCopyPaste.Droid  
    10. {  
    11.     public class CustomEntryRenderer : EntryRenderer  
    12.     {  
    13.         public CustomEntryRenderer(Context context) : base(context)  
    14.         {  
    15.         }  
    16.   
    17.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
    18.         {  
    19.             base.OnElementChanged(e);  
    20.   
    21.             if (Control != null)  
    22.             {  
    23.                 Control.CustomSelectionActionModeCallback = new Callback();  
    24.             }  
    25.         }  
    26.     }  
    27.   
    28.     // Following class is for hiding Menu control options (Cut, Copy, Paste ... etc).  
    29.     internal class Callback : Java.Lang.Object, ActionMode.ICallback  
    30.     {  
    31.   
    32.         public bool OnActionItemClicked(ActionMode mode, IMenuItem item)  
    33.         {  
    34.             return false;  
    35.         }  
    36.   
    37.         public bool OnCreateActionMode(ActionMode mode, IMenu menu)  
    38.         {  
    39.             return false;  
    40.         }  
    41.   
    42.         public void OnDestroyActionMode(ActionMode mode)  
    43.         {  
    44.   
    45.         }  
    46.   
    47.         public bool OnPrepareActionMode(ActionMode mode, IMenu menu)  
    48.         {  
    49.             return false;  
    50.         }  
    51.     }  
    52. }  
  1. Creating the custom renderer for iOS platform as shown below.
    1. using DisableCutCopyPaste;  
    2. using DisableCutCopyPaste.iOS;  
    3. using System.ComponentModel;  
    4. using UIKit;  
    5. using Xamarin.Forms;  
    6. using Xamarin.Forms.Platform.iOS;  
    7. using ObjCRuntime;  
    8. using Foundation;  
    9.   
    10. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
    11. namespace DisableCutCopyPaste.iOS  
    12. {  
    13.     public class CustomEntryRenderer : EntryRenderer  
    14.     {  
    15.         protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
    16.         {  
    17.             base.OnElementPropertyChanged(sender, e);  
    18.   
    19.             if (Control != null)  
    20.             {  
    21.                 // Write your logic if requires.  
    22.             }  
    23.         }  
    24.   
    25.         // Following method is for hiding Menu control options (Cut, Copy, Paste ... etc).  
    26.         public override bool CanPerform(Selector action, NSObject withSender)  
    27.         {  
    28.             NSOperationQueue.MainQueue.AddOperation(() =>  
    29.             {  
    30.                 UIMenuController.SharedMenuController.SetMenuVisible(falsefalse);  
    31.             });  
    32.   
    33.             return base.CanPerform(action, withSender);  
    34.         }  
    35.     }  
    36. }  
  1. Now, we have to use customized control in the xaml like below. So that the entry control will not display menu options on long tapping/clicking.
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
    3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
    4.              xmlns:local="clr-namespace:DisableCutCopyPaste"  
    5.              x:Class="DisableCutCopyPaste.MainPage">  
    6.   
    7.     <StackLayout HorizontalOptions="FillAndExpand"  
    8.                  VerticalOptions="FillAndExpand">  
    9.         <Label Text="Bank Account Details"   
    10.                Font="18"  
    11.                FontAttributes="Bold"  
    12.                HorizontalOptions="Center" />  
    13.   
    14.         <local:CustomEntry Placeholder="Enter account number"  
    15.                            HorizontalOptions="FillAndExpand"></local:CustomEntry>  
    16.   
    17.         <local:CustomEntry Placeholder="Re enter account number"  
    18.                            HorizontalOptions="FillAndExpand"></local:CustomEntry>  
    19.     </StackLayout>  
    20.   
    21. </ContentPage>  

To view full sourcecode, please click here.

Summary

Right-click on “DisableCutCopyPaste.Android” project and choose “Set as StartUp Project” so that the project name will become bold. Now, run the application so it will display like below.

Xamarin

If you see the above picture, I have two fields for entering bank account numbers. I have entered the account number in the first field and tried to copy it but it's not showing the options to copy or cut.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now