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.
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.
- We can restrict the user from copy-pasting the text in case of Password and Confirm password.
- 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#
- var entry = new Entry();
- 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.
- On the Start screen, launch Visual Studio. This opens the start page of Visual Studio like below.
- In Visual Studio, click "Create new project…" (Yellow highlighted section) to create a new project.
- 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:
- Create a class with some name (CustomEntry) in PCL/NET Standard project.
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Xamarin.Forms;
-
- namespace DisableCutCopyPaste
- {
- public class CustomEntry : Entry
- {
- }
- }
- Creating the custom renderer for Android platform as shown below.
- using Android.Content;
- using Android.Views;
- using DisableCutCopyPaste;
- using DisableCutCopyPaste.Droid;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.Android;
-
- [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
- namespace DisableCutCopyPaste.Droid
- {
- public class CustomEntryRenderer : EntryRenderer
- {
- public CustomEntryRenderer(Context context) : base(context)
- {
- }
-
- protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
- {
- base.OnElementChanged(e);
-
- if (Control != null)
- {
- Control.CustomSelectionActionModeCallback = new Callback();
- }
- }
- }
-
-
- internal class Callback : Java.Lang.Object, ActionMode.ICallback
- {
-
- public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
- {
- return false;
- }
-
- public bool OnCreateActionMode(ActionMode mode, IMenu menu)
- {
- return false;
- }
-
- public void OnDestroyActionMode(ActionMode mode)
- {
-
- }
-
- public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
- {
- return false;
- }
- }
- }
- Creating the custom renderer for iOS platform as shown below.
- using DisableCutCopyPaste;
- using DisableCutCopyPaste.iOS;
- using System.ComponentModel;
- using UIKit;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- using ObjCRuntime;
- using Foundation;
-
- [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
- namespace DisableCutCopyPaste.iOS
- {
- public class CustomEntryRenderer : EntryRenderer
- {
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- base.OnElementPropertyChanged(sender, e);
-
- if (Control != null)
- {
-
- }
- }
-
-
- public override bool CanPerform(Selector action, NSObject withSender)
- {
- NSOperationQueue.MainQueue.AddOperation(() =>
- {
- UIMenuController.SharedMenuController.SetMenuVisible(false, false);
- });
-
- return base.CanPerform(action, withSender);
- }
- }
- }
- 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.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:DisableCutCopyPaste"
- x:Class="DisableCutCopyPaste.MainPage">
-
- <StackLayout HorizontalOptions="FillAndExpand"
- VerticalOptions="FillAndExpand">
- <Label Text="Bank Account Details"
- Font="18"
- FontAttributes="Bold"
- HorizontalOptions="Center" />
-
- <local:CustomEntry Placeholder="Enter account number"
- HorizontalOptions="FillAndExpand"></local:CustomEntry>
-
- <local:CustomEntry Placeholder="Re enter account number"
- HorizontalOptions="FillAndExpand"></local:CustomEntry>
- </StackLayout>
-
- </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.
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.