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. namespace XamarinCustomEntry.Controls
  4. {
  5. public class CustomEntry:Entry
  6. {
  7. public new event EventHandler Completed;
  8. public static readonly BindableProperty ReturnTypeProperty = BindableProperty.Create(
  9. nameof(ReturnType),
  10. typeof(ReturnType),
  11. typeof(CustomEntry),
  12. ReturnType.Done,
  13. BindingMode.OneWay
  14. );
  15. public ReturnType ReturnType
  16. {
  17. get { return (ReturnType)GetValue(ReturnTypeProperty); }
  18. set { SetValue(ReturnTypeProperty, value); }
  19. }
  20. public void InvokeCompleted()
  21. {
  22. if (this.Completed != null)
  23. this.Completed.Invoke(this, null);
  24. }
  25. }
  26. public enum ReturnType
  27. {
  28. Go,
  29. Next,
  30. Done,
  31. Send,
  32. Search
  33. }
  34. }

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. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
  11. namespace XamarinCustomEntry.Droid
  12. {
  13. public class CustomEntryRenderer : EntryRenderer
  14. {
  15. public CustomEntryRenderer(Android.Content.Context context) : base(context)
  16. {
  17. AutoPackage = false;
  18. }
  19. protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
  20. {
  21. base.OnElementChanged(e);
  22. CustomEntry entry = (CustomEntry)this.Element;
  23. if (this.Control != null)
  24. {
  25. if (entry != null)
  26. {
  27. SetReturnType(entry);
  28. // Editor Action is called when the return button is pressed
  29. Control.EditorAction += (object sender, TextView.EditorActionEventArgs args) =>
  30. {
  31. if (entry.ReturnType != Controls.ReturnType.Next)
  32. entry.Unfocus();
  33. // Call all the methods attached to base_entry event handler Completed
  34. entry.InvokeCompleted();
  35. };
  36. }
  37. }
  38. }
  39. private void SetReturnType(CustomEntry entry)
  40. {
  41. Controls.ReturnType type = entry.ReturnType;
  42. switch (type)
  43. {
  44. case Controls.ReturnType.Go:
  45. Control.ImeOptions = ImeAction.Go;
  46. Control.SetImeActionLabel("Go", ImeAction.Go);
  47. break;
  48. case Controls.ReturnType.Next:
  49. Control.ImeOptions = ImeAction.Next;
  50. Control.SetImeActionLabel("Next", ImeAction.Next);
  51. break;
  52. case Controls.ReturnType.Send:
  53. Control.ImeOptions = ImeAction.Send;
  54. Control.SetImeActionLabel("Send", ImeAction.Send);
  55. break;
  56. case Controls.ReturnType.Search:
  57. Control.ImeOptions = ImeAction.Search;
  58. Control.SetImeActionLabel("Search", ImeAction.Search);
  59. break;
  60. default:
  61. Control.ImeOptions = ImeAction.Done;
  62. Control.SetImeActionLabel("Done", ImeAction.Done);
  63. break;
  64. }
  65. }
  66. }
  67. }

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. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
  9. namespace XamarinCustomEntry.iOS
  10. {
  11. public class CustomEntryRenderer: EntryRenderer
  12. {
  13. protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
  14. {
  15. base.OnElementChanged(e);
  16. CustomEntry entry = (CustomEntry)this.Element;
  17. if (this.Control != null)
  18. {
  19. if (entry != null)
  20. {
  21. SetReturnType(entry);
  22. Control.ShouldReturn += (UITextField tf) =>
  23. {
  24. entry.InvokeCompleted();
  25. return true;
  26. };
  27. }
  28. }
  29. }
  30. private void SetReturnType(CustomEntry entry)
  31. {
  32. Controls.ReturnType type = entry.ReturnType;
  33. switch (type)
  34. {
  35. case Controls.ReturnType.Go:
  36. Control.ReturnKeyType = UIReturnKeyType.Go;
  37. break;
  38. case Controls.ReturnType.Next:
  39. Control.ReturnKeyType = UIReturnKeyType.Next;
  40. break;
  41. case Controls.ReturnType.Send:
  42. Control.ReturnKeyType = UIReturnKeyType.Send;
  43. break;
  44. case Controls.ReturnType.Search:
  45. Control.ReturnKeyType = UIReturnKeyType.Search;
  46. break;
  47. case Controls.ReturnType.Done:
  48. Control.ReturnKeyType = UIReturnKeyType.Done;
  49. break;
  50. default:
  51. Control.ReturnKeyType = UIReturnKeyType.Default;
  52. break;
  53. }
  54. }
  55. }
  56. }

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. <Image Margin="0,100,0,0" Source="banner1.png"></Image>
  9. <controls:CustomEntry ReturnType="Search" Placeholder="Search"></controls:CustomEntry>
  10. <controls:CustomEntry ReturnType="Done" Placeholder="Done"></controls:CustomEntry>
  11. </StackLayout>
  12. </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 :)