Xamarin.Forms - Change Entry Return Button

Introduction

 
Xamarin.Forms - Change Entry Return Button
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

Custom Renderers

 
Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
 
Xamarin.Forms - Change Entry Return Button
 
Now, you need to click "Create a new project".
 
Now, filter by Project Type: Mobile.
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP). 
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the "Play" button to try it out.
 

Create a Custom Entry

 
Now, create an Inherit class form Entry for customizing the Entry control.
 
Now, write the following code.
 
CustomEntry.cs
  1. using System;  
  2. using Xamarin.Forms;  
  3.   
  4. namespace XamarinCustomEntry.Controls  
  5. {  
  6.     public class CustomEntry:Entry  
  7.     {  
  8. public new event EventHandler Completed;  
  9.   
  10.         public static readonly BindableProperty ReturnTypeProperty = BindableProperty.Create(  
  11.             nameof(ReturnType),  
  12.             typeof(ReturnType),  
  13.             typeof(CustomEntry),  
  14.             ReturnType.Done,  
  15.             BindingMode.OneWay  
  16.         );  
  17.   
  18.         public ReturnType ReturnType  
  19.         {  
  20.             get { return (ReturnType)GetValue(ReturnTypeProperty); }  
  21.             set { SetValue(ReturnTypeProperty, value); }  
  22.         }  
  23.   
  24.         public void InvokeCompleted()  
  25.         {  
  26.             if (this.Completed != null)  
  27.                 this.Completed.Invoke(thisnull);  
  28.         }  
  29.     }  
  30.     public enum ReturnType  
  31.     {  
  32.   
  33.         Go,  
  34.         Next,  
  35.         Done,  
  36.         Send,  
  37.         Search  
  38.     }  
  39. }  

Android Implementation

 
In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.
 
Now, write the code given below.
 
CustomEntryRenderer.cs
  1. using System;  
  2. using System.Runtime.Remoting.Contexts;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using XamarinCustomEntry.Controls;  
  6. using XamarinCustomEntry.Droid;  
  7. using Android.Content;  
  8. using Android.Widget;  
  9. using Android.Views.InputMethods;  
  10.   
  11. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
  12. namespace XamarinCustomEntry.Droid  
  13. {  
  14.     public class CustomEntryRenderer : EntryRenderer  
  15.     {  
  16.         public CustomEntryRenderer(Android.Content.Context context) : base(context)  
  17.         {  
  18.             AutoPackage = false;  
  19.         }  
  20.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
  21.         {  
  22.             base.OnElementChanged(e);  
  23.   
  24.             CustomEntry entry = (CustomEntry)this.Element;  
  25.   
  26.             if (this.Control != null)  
  27.             {  
  28.                 if (entry != null)  
  29.                 {  
  30.                     SetReturnType(entry);  
  31.   
  32.                     // Editor Action is called when the return button is pressed  
  33.                     Control.EditorAction += (object sender, TextView.EditorActionEventArgs args) =>  
  34.                     {  
  35.                         if (entry.ReturnType != Controls.ReturnType.Next)  
  36.                             entry.Unfocus();  
  37.   
  38.                         // Call all the methods attached to base_entry event handler Completed  
  39.                         entry.InvokeCompleted();  
  40.                     };  
  41.                 }  
  42.             }  
  43.         }  
  44.         private void SetReturnType(CustomEntry entry)  
  45.         {  
  46.             Controls.ReturnType type = entry.ReturnType;  
  47.   
  48.             switch (type)  
  49.             {  
  50.                 case Controls.ReturnType.Go:  
  51.                     Control.ImeOptions = ImeAction.Go;  
  52.                     Control.SetImeActionLabel("Go", ImeAction.Go);  
  53.                     break;  
  54.                 case Controls.ReturnType.Next:  
  55.                     Control.ImeOptions = ImeAction.Next;  
  56.                     Control.SetImeActionLabel("Next", ImeAction.Next);  
  57.                     break;  
  58.                 case Controls.ReturnType.Send:  
  59.                     Control.ImeOptions = ImeAction.Send;  
  60.                     Control.SetImeActionLabel("Send", ImeAction.Send);  
  61.                     break;  
  62.                 case Controls.ReturnType.Search:  
  63.                     Control.ImeOptions = ImeAction.Search;  
  64.                     Control.SetImeActionLabel("Search", ImeAction.Search);  
  65.                     break;  
  66.                 default:  
  67.                     Control.ImeOptions = ImeAction.Done;  
  68.                     Control.SetImeActionLabel("Done", ImeAction.Done);  
  69.                     break;  
  70.             }  
  71.         }  
  72.     }  
  73. }  

iOS Implementation

 
In this step, create an inherit Class form, EntryRenderer, for customizing the Entry control.
 
Now, write the code given below.
 
CustomEntryRenderer.cs
  1. using System;  
  2. using System.ComponentModel;  
  3. using UIKit;  
  4. using Xamarin.Forms;  
  5. using Xamarin.Forms.Platform.iOS;  
  6. using XamarinCustomEntry.Controls;  
  7. using XamarinCustomEntry.iOS;  
  8.   
  9. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
  10. namespace XamarinCustomEntry.iOS  
  11. {  
  12.     public class CustomEntryRenderer: EntryRenderer  
  13.     {  
  14.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
  15.         {  
  16.             base.OnElementChanged(e);  
  17.             CustomEntry entry = (CustomEntry)this.Element;  
  18.             if (this.Control != null)  
  19.             {  
  20.                 if (entry != null)  
  21.                 {  
  22.                     SetReturnType(entry);  
  23.                     Control.ShouldReturn += (UITextField tf) =>  
  24.                     {  
  25.                         entry.InvokeCompleted();  
  26.                         return true;  
  27.                     };  
  28.                 }  
  29.             }  
  30.         }  
  31.   
  32.         private void SetReturnType(CustomEntry entry)  
  33.         {  
  34.             Controls.ReturnType type = entry.ReturnType;  
  35.             switch (type)  
  36.             {  
  37.                 case Controls.ReturnType.Go:  
  38.                     Control.ReturnKeyType = UIReturnKeyType.Go;  
  39.                     break;  
  40.                 case Controls.ReturnType.Next:  
  41.                     Control.ReturnKeyType = UIReturnKeyType.Next;  
  42.                     break;  
  43.                 case Controls.ReturnType.Send:  
  44.                     Control.ReturnKeyType = UIReturnKeyType.Send;  
  45.                     break;  
  46.                 case Controls.ReturnType.Search:  
  47.                     Control.ReturnKeyType = UIReturnKeyType.Search;  
  48.                     break;  
  49.                 case Controls.ReturnType.Done:  
  50.                     Control.ReturnKeyType = UIReturnKeyType.Done;  
  51.                     break;  
  52.                 default:  
  53.                     Control.ReturnKeyType = UIReturnKeyType.Default;  
  54.                     break;  
  55.             }  
  56.         }  
  57.   
  58.     }  
  59. }  

Setting up the User Interface

 
Go to MainPage.Xaml and write the following code.
 
MainPage.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:local="clr-namespace:XamarinCustomEntry"   
  5.              x:Class="XamarinCustomEntry.MainPage"  
  6.              xmlns:controls="clr-namespace:XamarinCustomEntry.Controls">  
  7.     <StackLayout>  
  8.           
  9.         <Image Margin="0,100,0,0" Source="banner1.png"></Image>  
  10.         <controls:CustomEntry ReturnType="Search" Placeholder="Search"></controls:CustomEntry>  
  11.         <controls:CustomEntry ReturnType="Done" Placeholder="Done"></controls:CustomEntry>  
  12.     </StackLayout>  
  13. </ContentPage>   
Click the "Play" button to try it out.
 
Xamarin.Forms - Change Entry Return Button  
 
I hope you have understood how to change entry return button in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles