How To Disable Copy And Paste Options For Entry In Xamarin.Forms Using CustomRenderer

Introduction

Sometimes, for security reasons, we need to restrict the copy, cut, and paste to clipboard options for entry in our app, for example - adding payee through Online Banking and while entering passwords. So, in this article, we will learn how to disable those options for Entry using CustomRenderer concept in Xamarin.Forms.

In my previous article I explained Copy and Paste using Clipboard in Xamarin.Forms.

Requirements

  • This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  • This article is prepared on a MAC machine.
  • This sample project is Xamarin.Forms PCL project.
  • This sample app is targeted for Android & iOS and, tested for Android & iOS.

Description

First, follow the below steps to create the new Xamarin.Forms project.

  1. Open Visual Studio for Mac.
  2. Click on the File menu and select New Solution.
  3. In the left pane of the dialog, let's select the type of templates to display. Multiplatform > App > Xamarin.Forms > Blank Forms App and click "Next".
  4. Next, enter your app name (Ex: DisableClipboardOperationsdemo). At the bottom, select target platforms to Android & iOS and shared code to Portable Class Library and click the "Next" button.
  5. Then, choose project location with the help of Browse button and click "Create".
Now, the project structure will be created like below.
  • DisableClipboardOperationsdemo
    It is for Shared Code
  • DisableClipboardOperationsdemo.Droid
    It is for Android.

  • DisableClipboardOperationsdemo.iOS
    It is for iOS. 

Now, follow the below steps.

Portable Class Library (PCL)

Step 1:

In PCL, create a class name  HideClipboardEntry inside the CustomRenderers folder and it should inherit from Entry like below.

HideClipboardEntry.cs

  1. using Xamarin.Forms;  
  2. namespace DisableClipboardOperationsDemo.CustomRenderers {  
  3.     public class HideClipboardEntry: Entry { }  
  4.  

Step 2:

Create your own XAML page named HideClipboardPage.xaml inside the Views folder and make sure to refer to "HideClipboardEntry" class in XAML by declaring a namespace for its location and using the namespace prefix on the control element. The following code example shows how the "HideClipboardEntry" renderer class can be consumed by an XAML page:

HideClipboardPage.xaml

  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:customCtrl="clr-namespace:DisableClipboardOperationsdemo.CustomRenderers" 
  5. x:Class="DisableClipboardOperationsdemo.Views.HideClipboardPage" 
  6. BackgroundColor="#533F95">  
  7.     <ContentPage.Content>  
  8.         <StackLayout HorizontalOptions="FillAndExpand" Padding="30">  
  9.             <Label Text="Disable Clipboard operations(Copy, Cut and Paste)" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" TextColor="White" FontSize="20" />  
  10.             <StackLayout VerticalOptions="CenterAndExpand" Spacing="15">  
  11.                 <customCtrl:HideClipboardEntry Placeholder="Payee account number" PlaceholderColor="#533F95" HeightRequest="40" HorizontalOptions="FillAndExpand" BackgroundColor="White" />  
  12.                 <customCtrl:HideClipboardEntry Placeholder="Confirm payee account number" PlaceholderColor="#533F95" HeightRequest="40" HorizontalOptions="FillAndExpand" BackgroundColor="White" /> </StackLayout>  
  13.         </StackLayout>  
  14.     </ContentPage.Content>  
  15. </ContentPage>  

Note - The "customCtrl" namespace prefix can be named anything. However, the clr-namespace and assembly values must match the details of the custom renderer class. Once the namespace is declared the prefix is used to reference the custom control.

HideClipboardPage.xaml.cs

  1. using Xamarin.Forms;  
  2.   
  3. namespace DisableClipboardOperationsdemo.Views  
  4. {  
  5.     public partial class HideClipboardPage : ContentPage  
  6.     {  
  7.         public HideClipboardPage()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.     }  
  12. }   

Xamarin.Andriod

In an Android project, create a class and add the code like below.

HideClipboardEntryRenderer.cs

  1. using Android.Content;  
  2. using Android.Views;  
  3. using DisableClipboardOperationsDemo.CustomRenderers;  
  4. using DisableClipboardOperationsDemo.Droid;  
  5. using Xamarin.Forms;  
  6. using Xamarin.Forms.Platform.Android;  
  7. [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]  
  8. namespace DisableClipboardOperationsDemo.Droid {  
  9.     public class HideClipboardEntryRenderer: EntryRenderer {  
  10.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {  
  11.             base.OnElementChanged(e);  
  12.             if (Control != null) {  
  13.                 Control.CustomSelectionActionModeCallback = new Callback();  
  14.                 Control.LongClickable = false;  
  15.             }  
  16.         }  
  17.     }  
  18.     public class Callback: Java.Lang.Object, ActionMode.ICallback {  
  19.         public bool OnActionItemClicked(ActionMode mode, IMenuItem item) {  
  20.             return false;  
  21.         }  
  22.         public bool OnCreateActionMode(ActionMode mode, IMenu menu) {  
  23.             return false;  
  24.         }  
  25.         public void OnDestroyActionMode(ActionMode mode) {}  
  26.         public bool OnPrepareActionMode(ActionMode mode, IMenu menu) {  
  27.             return false;  
  28.         }  
  29.     }  

Xamarin.iOS

In iOS project, create a class and add the code like below.

HideClipboardEntryRenderer.cs

  1. using DisableClipboardOperationsDemo.iOS;  
  2. using UIKit;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Platform.iOS;  
  5. using ObjCRuntime;  
  6. using Foundation;  
  7. using DisableClipboardOperationsDemo.CustomRenderers;  
  8. [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]  
  9. namespace DisableClipboardOperationsDemo.iOS {  
  10.     public class HideClipboardEntryRenderer: EntryRenderer {  
  11.         public override bool CanPerform(Selector action, NSObject withSender) {  
  12.             MainQueue.AddOperation(() => {  
  13.                 UIMenuController.SharedMenuController.SetMenuVisible(falsefalse);  
  14.             });  
  15.             return base.CanPerform(action, withSender);  
  16.         }  
  17.     }  
  18. }  

Output:

Please download the source code from here.

 


Similar Articles