Tuesday 22 May 2018

How to hide menu options (Cut, Copy, Paste ... etc ) for Xamarin Forms Entry

Hi Everyone,
In this post I will gonna be explaining about hiding the menu options like Cut, Copy, Paste ... etc for Xamarin Forms Entry.

When it is useful ?
  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 the bank account details ... etc

We can hide Cut, Copy, Paste ... ect options by using Custom Renderers concept of Xamarin Forms.
Follow the below steps to achieve it.

·         Create a project with name “DisableCutCopyPaste” and then follow below steps.
·         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 in 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();
            }
        }
    }

    // Following class is for hiding Menu control options (Cut, Copy, Paste ... etc).
    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 in 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)
            {
                // Write your logic if requires.
            }
        }

        // Following method is for hiding Menu control options (Cut, Copy, Paste ... etc).
        public override bool CanPerform(Selector action, NSObject withSender)
        {
            NSOperationQueue.MainQueue.AddOperation(() =>
            {
                UIMenuController.SharedMenuController.SetMenuVisible(false, false);
            });

            return base.CanPerform(action, withSender);
        }
    }
}

Now, use customized control in the Xaml pages wherever you requires like below. So that the entry control will not display menu option on long tapping.
<?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>

If you need full source code, please click here
If you have any doubts in this post, please raise your questions in comment section.

Thanks for reading the article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.