Introduction

Today, I would like to tell you about Entry Key ReturnType. I'll tell you how to change ReturnKeyType keys like Search, Done, Next etc. In this article, I am using Xamarin.Forms PORTABLE and XAML.

Entry handles the Keyboard return key description.

Implementation

Open Visual Studio and select a "New Project".

Xamarin

Select Cross-Platform App, give the project a name, and set the project path. After that, click OK.

Xamarin

Select the template as "Blank App" and code sharing as "PCL".

Xamarin

Right-click on PCL project and select Add >> New Item or Add >> Class.

Xamarin

Now, we are creating a class CustomKeyEntry.cs and write the following C# code.

Xamarin

CustomKeyEntry.cs

  1. public class CustomKeyEntry : Entry
  2. {
  3. // Need to overwrite default handler because we cant Invoke otherwise
  4. public new event EventHandler Completed;
  5. public static readonly BindableProperty ReturnTypeProperty = BindableProperty.Create(nameof(ReturnType),
  6. typeof(ReturnType), typeof(CustomKeyEntry),
  7. ReturnType.Done, BindingMode.OneWay);
  8. public ReturnType ReturnType
  9. {
  10. get { return (ReturnType)GetValue(ReturnTypeProperty); }
  11. set { SetValue(ReturnTypeProperty, value); }
  12. }
  13. public void InvokeCompleted()
  14. {
  15. if (this.Completed != null)
  16. this.Completed.Invoke(this, null);
  17. }
  18. }
  19. public enum ReturnType
  20. {
  21. Go,
  22. Next,
  23. Done,
  24. Send,
  25. Search
  26. }
  27. }

We need to set ReturnType property in Android and iOS project.

Let us start with Android….

We have to create a Class in Android Project and render the entry.

Then, we set all the properties during the initialization of PCL Project.

Xamarin

Please make sure to add dependency ([assembly: ExportRenderer(typeof(CustomKeyEntry), typeof(CustomKeyEntryRenderer))]) of Android (CustomEntryRndered) and PCL(CustomEntry).

CustomKeyEntryRenderer.cs

  1. public class CustomKeyEntryRenderer : EntryRenderer
  2. {
  3. protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
  4. {
  5. base.OnElementChanged(e);
  6. CustomKeyEntry entry = (CustomKeyEntry)this.Element;
  7. if (this.Control != null)
  8. {
  9. if (entry != null)
  10. {
  11. SetReturnType(entry);
  12. // Editor Action is called when the return button is pressed
  13. Control.EditorAction += (object sender, TextView.EditorActionEventArgs args) =>
  14. {
  15. if (entry.ReturnType != ReturnType.Next)
  16. entry.Unfocus();
  17. // Call all the methods attached to custom_entry event handler Completed
  18. entry.InvokeCompleted();
  19. };
  20. }
  21. }
  22. }
  23. private void SetReturnType(CustomKeyEntry entry)
  24. {
  25. ReturnType type = entry.ReturnType;
  26. switch (type)
  27. {
  28. case ReturnType.Go:
  29. Control.ImeOptions = Android.Views.InputMethods.ImeAction.Go;
  30. Control.SetImeActionLabel("Go", ImeAction.Go);
  31. break;
  32. case ReturnType.Next:
  33. Control.ImeOptions = ImeAction.Next;
  34. Control.SetImeActionLabel("Next", ImeAction.Next);
  35. break;
  36. case ReturnType.Send:
  37. Control.ImeOptions = ImeAction.Send;
  38. Control.SetImeActionLabel("Send", ImeAction.Send);
  39. break;
  40. case ReturnType.Search:
  41. Control.ImeOptions = ImeAction.Search;
  42. Control.SetImeActionLabel("Search", ImeAction.Search);
  43. break;
  44. default:
  45. Control.ImeOptions = ImeAction.Done;
  46. Control.SetImeActionLabel("Done", ImeAction.Done);
  47. break;
  48. }
  49. }
  50. }

Let’s come to iOS project. First, set the PCL(CustomEntry) property in iOS Project.

Create a class by right-clicking on iOS Project and select Apple. Then select "Class" and give this class a name as CustomEntryRendered.cs.

Xamarin

Please make sure to add dependancy [assembly: ExportRenderer(typeof(CustomKeyEntry), typeof(CustomKeyEntryRenderer))].

Now, let’s write some code for Entry and set the property.

CustomKeyEntryRenderer.cs

  1. public class CustomKeyEntryRenderer : EntryRenderer
  2. {
  3. protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
  4. {
  5. base.OnElementChanged(e);
  6. CustomKeyEntry entry = (CustomKeyEntry)this.Element;
  7. if (this.Control != null)
  8. {
  9. if (entry != null)
  10. {
  11. SetReturnType(entry);
  12. Control.ShouldReturn += (UITextField tf) =>
  13. {
  14. entry.InvokeCompleted();
  15. return true;
  16. };
  17. }
  18. }
  19. }
  20. private void SetReturnType(CustomKeyEntry entry)
  21. {
  22. ReturnType type = entry.ReturnType;
  23. switch (type)
  24. {
  25. case ReturnType.Go:
  26. Control.ReturnKeyType = UIReturnKeyType.Go;
  27. break;
  28. case ReturnType.Next:
  29. Control.ReturnKeyType = UIReturnKeyType.Next;
  30. break;
  31. case ReturnType.Send:
  32. Control.ReturnKeyType = UIReturnKeyType.Send;
  33. break;
  34. case ReturnType.Search:
  35. Control.ReturnKeyType = UIReturnKeyType.Search;
  36. break;
  37. case ReturnType.Done:
  38. Control.ReturnKeyType = UIReturnKeyType.Done;
  39. break;
  40. default:
  41. Control.ReturnKeyType = UIReturnKeyType.Default;
  42. break;
  43. }
  44. }
  45. }

Now, go to the PCL Project and write this code in MainPage.xaml.

Xamarin

As you can see in the above code, we have to set the view reference in xmlns:custom="clr-namespace:App1.CustomViews.KeyEntry" MainPage.xaml.

Write the following code for CustomKeyEntry.

MainPage.xaml

  1. <StackLayout Spacing="20" Padding="20">
  2. <key:CustomKeyEntry Placeholder="Done Key" ReturnType="Done" />
  3. <key:CustomKeyEntry Placeholder="Go Key" ReturnType="Go" />
  4. <key:CustomKeyEntry Placeholder="Next Key" ReturnType="Next" />
  5. <key:CustomKeyEntry Placeholder="Search Key" ReturnType="Search" />
  6. <key:CustomKeyEntry Placeholder="Send Key" ReturnType="Send" />
  7. </StackLayout>

TADDAAAAA! :)

Now, your Custom Key Entry working!! If you want to watch this video tutorial, click here

Features of CustomReturnType controls

Custom ReturnType Property=(ReturnType="Search")
and all public entry controls are available in this CustomKeyEntry.

Keep Share and Happy Coding...
:)