From 0dae5b1782a95ef515b017557fda4f4c1f3a4960 Mon Sep 17 00:00:00 2001 From: Kaustav Chaudhuri Date: Mon, 8 May 2023 17:30:20 +0530 Subject: [PATCH] Custom Renderer Implementation for Android --- .../NoUnderlineEntryRenderer.cs | 32 +++++++++++++ .../CustomRenderer/OtpEntryRenderer.cs | 45 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/NoUnderlineEntryRenderer.cs create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/OtpEntryRenderer.cs diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/NoUnderlineEntryRenderer.cs b/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/NoUnderlineEntryRenderer.cs new file mode 100644 index 0000000..f2864a4 --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/NoUnderlineEntryRenderer.cs @@ -0,0 +1,32 @@ +using Android.Content; +using Android.Graphics.Drawables; +using GMCabsDriverAssistantSolution.CustomControls; +using GMCabsDriverAssistantSolution.Platforms.Android.CustomRenderer; +using Microsoft.Maui.Controls.Compatibility; +using Microsoft.Maui.Controls.Compatibility.Platform.Android; +using Microsoft.Maui.Controls.Platform; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +[assembly: ExportRenderer(typeof(NoUnderlineEntry), typeof(NoUnderlineEntryRenderer))] +namespace GMCabsDriverAssistantSolution.Platforms.Android.CustomRenderer +{ + public class NoUnderlineEntryRenderer : EntryRenderer + { + public NoUnderlineEntryRenderer(Context context) : base(context) + { + AutoPackage = false; + } + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + if (Control != null) + { + Control.Background = new ColorDrawable(global::Android.Graphics.Color.Transparent); + } + } + } +} diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/OtpEntryRenderer.cs b/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/OtpEntryRenderer.cs new file mode 100644 index 0000000..9e6aa89 --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/CustomRenderer/OtpEntryRenderer.cs @@ -0,0 +1,45 @@ +using Android.Content; +using Android.Views; +using GMCabsDriverAssistantSolution.CustomControls; +using GMCabsDriverAssistantSolution.Platforms.Android.CustomRenderer; +using Microsoft.Maui.Controls.Compatibility; +using Microsoft.Maui.Controls.Compatibility.Platform.Android; +using Microsoft.Maui.Controls.Platform; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +[assembly: ExportRenderer(typeof(OtpEntry), typeof(OtpEntryRenderer))] +namespace GMCabsDriverAssistantSolution.Platforms.Android.CustomRenderer +{ + public class OtpEntryRenderer : EntryRenderer + { + public OtpEntryRenderer(Context context) : base(context) + { + } + + public override bool DispatchKeyEvent(KeyEvent e) + { + if (e.Action == KeyEventActions.Down) + { + if (e.KeyCode == Keycode.Del) + { + if (string.IsNullOrWhiteSpace(Control.Text)) + { + var entry = (OtpEntry)Element; + entry.OnBackspacePressed(); + } + } + } + return base.DispatchKeyEvent(e); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + } + } +}