Saturday, 26 May 2018

How to disable copy, cut and paste options for Entry text in Xamarin.Forms

Introduction 
Sometimes for security reasons, we need to restrict the copy, cut and paste clipboard options for Entry in our app. For example adding payee through Online Banking and while entering passwords. So, in this article we can learn how to disable those options for Entry using CustomRenderers concept in Xamarin.Forms.
In my previous article 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.       
  3. namespace DisableClipboardOperationsdemo.CustomRenderers      
  4. {      
  5.     public class HideClipboardEntry : Entry      
  6.     {      
  7.              
  8.     }      
  9. }      
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"     
  9.             Padding="30">    
  10.             <Label Text="Disable Clipboard operations(Copy, Cut and Paste)"    
  11.                 HorizontalTextAlignment="Center"     
  12.                 HorizontalOptions="CenterAndExpand"     
  13.                 TextColor="White" FontSize="20"/>    
  14.             <StackLayout VerticalOptions="CenterAndExpand"     
  15.                 Spacing="15">    
  16.                 <customCtrl:HideClipboardEntry     
  17.                     Placeholder="Payee account number"     
  18.                     PlaceholderColor="#533F95"     
  19.                     HeightRequest="40"     
  20.                     HorizontalOptions="FillAndExpand"  
  21.                     BackgroundColor="White"/>    
  22.                 <customCtrl:HideClipboardEntry     
  23.                     Placeholder="Confirm payee account number"     
  24.                     PlaceholderColor="#533F95"      
  25.                     HeightRequest="40"     
  26.                     HorizontalOptions="FillAndExpand"  
  27.                     BackgroundColor="White"/>    
  28.             </StackLayout>    
  29.         </StackLayout>    
  30.     </ContentPage.Content>    
  31. </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.     
  8. [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]    
  9. namespace DisableClipboardOperationsdemo.Droid    
  10. {    
  11.     public class HideClipboardEntryRenderer : EntryRenderer{    
  12.     
  13.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e){    
  14.             base.OnElementChanged(e);    
  15.     
  16.             if (Control != null){    
  17.                 Control.CustomSelectionActionModeCallback = new Callback();    
  18.                 Control.LongClickable = false;    
  19.             }    
  20.         }    
  21.     }    
  22.     
  23.     public class Callback : Java.Lang.Object, ActionMode.ICallback{    
  24.     
  25.         public bool OnActionItemClicked(ActionMode mode, IMenuItem item){    
  26.             return false;    
  27.         }    
  28.     
  29.         public bool OnCreateActionMode(ActionMode mode, IMenu menu){    
  30.             return false;    
  31.         }    
  32.     
  33.         public void OnDestroyActionMode(ActionMode mode){    
  34.     
  35.         }    
  36.     
  37.         public bool OnPrepareActionMode(ActionMode mode, IMenu menu){    
  38.             return false;    
  39.         }    
  40.     }    
  41. }    
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.     
  9. [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]    
  10. namespace DisableClipboardOperationsdemo.iOS    
  11. {    
  12.     public class HideClipboardEntryRenderer : EntryRenderer    
  13.     {    
  14.         public override bool CanPerform(Selector action, NSObject withSender)    
  15.         {    
  16.             NSOperationQueue.MainQueue.AddOperation(() =>    
  17.             {    
  18.                 UIMenuController.SharedMenuController.SetMenuVisible(falsefalse);    
  19.             });    
  20.     
  21.             return base.CanPerform(action, withSender);    
  22.         }    
  23.     }    
  24. }    
Output:
Please download source code from here

No comments:

Post a Comment