While developing login pages, we usually get a requirement that there should be an icon in password entry to show/hide password while entering the password. While looking for a solution to this requirement, I found that most of the implementations are done using custom controls whereas we can do this kind of small platform-specific customization using effects. Last year, I wrote this article about implementing custom fonts using effects, so I thought let's try this also using the same technique
Creating the Effect
Changes in PCL projectCreate a new class file named ‘ShowHidePassEffect’ and add the following code to it.
- public class ShowHidePassEffect : RoutingEffect
- {
- public string EntryText { get; set; }
- public ShowHidePassEffect() : base("Xamarin.ShowHidePassEffect"){}
- }
In the above mentioned code, we are creating a custom effect where we are creating a new property named ‘EntryText’ to show the entered password text. The implementation of this effect will have to be written in Platform specific projects.
Changes in Android projectIn your Android project, create a new class file named ‘ShowHidePassEffect’ and add the following code into it.
- using Android.Text.Method;
- using Android.Views;
- using Android.Widget;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.Android;
- [assembly: ResolutionGroupName("Xamarin")]
- [assembly: ExportEffect(typeof(ShowHidePassEx.Droid.Effects.ShowHidePassEffect), "ShowHidePassEffect")]
- namespace ShowHidePassEx.Droid.Effects
- {
- public class ShowHidePassEffect : PlatformEffect
- {
- protected override void OnAttached()
- {
- ConfigureControl();
- }
- protected override void OnDetached()
- {
- }
- private void ConfigureControl()
- {
- EditText editText = ((EditText)Control);
- editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.ShowPass, 0);
- editText.SetOnTouchListener(new OnDrawableTouchListener());
- }
- }
- public class OnDrawableTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
- {
- public bool OnTouch(Android.Views.View v, MotionEvent e)
- {
- if (v is EditText && e.Action == MotionEventActions.Up)
- {
- EditText editText = (EditText)v;
- if (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width()))
- {
- if (editText.TransformationMethod == null)
- {
- editText.TransformationMethod = PasswordTransformationMethod.Instance;
- editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.ShowPass, 0);
- }
- else
- {
- editText.TransformationMethod = null;
- editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.HidePass, 0);
- }
- return true;
- }
- }
- return false;
- }
- }
- }
Before writing this code, make sure that you have ShowPass and HidePass images in your Resources/Drawable folder of Android project, like in the following image.

These images can be downloaded from the internet, or your project’s designer can provide it.
Similarly, in your iOS project, create a new class file named ‘ShowHidePassEffect’ and add the following code.
- using System;
- using ShowHidePassEx.iOS.Effects;
- using UIKit;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- [assembly: ResolutionGroupName("Xamarin")]
- [assembly: ExportEffect(typeof(ShowHidePassEffect), "ShowHidePassEffect")]
- namespace ShowHidePassEx.iOS.Effects
- {
- public class ShowHidePassEffect: PlatformEffect
- {
- protected override void OnAttached()
- {
- ConfigureControl();
- }
- protected override void OnDetached()
- {
- }
- private void ConfigureControl()
- {
- if (Control != null)
- {
- UITextField vUpdatedEntry = (UITextField)Control;
- var buttonRect = UIButton.FromType(UIButtonType.Custom);
- buttonRect.SetImage(new UIImage("ShowPass"), UIControlState.Normal);
- buttonRect.TouchUpInside += (object sender, EventArgs e1) =>
- {
- if (vUpdatedEntry.SecureTextEntry)
- {
- vUpdatedEntry.SecureTextEntry = false;
- buttonRect.SetImage(new UIImage("HidePass"), UIControlState.Normal);
- }
- else
- {
- vUpdatedEntry.SecureTextEntry = true;
- buttonRect.SetImage(new UIImage("ShowPass"), UIControlState.Normal);
- }
- };
- vUpdatedEntry.ShouldChangeCharacters += (textField, range, replacementString) =>
- {
- string text = vUpdatedEntry.Text;
- var result = text.Substring(0, (int)range.Location) + replacementString + text.Substring((int)range.Location + (int)range.Length);
- vUpdatedEntry.Text = result;
- return false;
- };
- buttonRect.Frame = new CoreGraphics.CGRect(10.0f, 0.0f, 15.0f, 15.0f);
- buttonRect.ContentMode = UIViewContentMode.Right;
- UIView paddingViewRight = new UIView(new System.Drawing.RectangleF(5.0f, -5.0f, 30.0f, 18.0f));
- paddingViewRight.Add(buttonRect);
- paddingViewRight.ContentMode = UIViewContentMode.BottomRight;
- vUpdatedEntry.LeftView = paddingViewRight;
- vUpdatedEntry.LeftViewMode = UITextFieldViewMode.Always;
- Control.Layer.CornerRadius = 4;
- Control.Layer.BorderColor = new CoreGraphics.CGColor(255, 255, 255);
- Control.Layer.MasksToBounds = true;
- vUpdatedEntry.TextAlignment = UITextAlignment.Left;
- }
- }
- }
- }
Just like Android, here too, make sure that you have ShowPass and HidePass images in your Resources folder of iOS project.

Using the Effect
In order to use the effect, we will just have to add the effect to the entry control in XAML like in the following code.
- <Entry IsPassword="true" Placeholder="Password" Text="Password">
- <Entry.Effects>
- <ef:ShowHidePassEffect />
- </Entry.Effects>
- </Entry>
And, if you want to use it inside your C# code, you can do that like this.
- var vPasswordEntry = new Entry() { IsPassword=true };
- vPasswordEntry.Effects.Add(new FontEffect());
This is the sample application whose code is published on Github. It looks something like the following when executed.
iOS Simulator

Android Simulator

This is one simple tip to implement Show/Hide password feature in your Xamarin.Forms mobile apps. I will try to come up with more such tips in future. Let me know if I have missed anything or you have any suggestions/concerns/queries.

Raynaldo Kristiadi Sola GratiaPosted May 15, 2020, 10:08 PM
I tried putting the entry inside a grid. But when i tapped on the entry, it trigger the show/hide password event instead. So the entry on focus event which normally show android keyboard is not triggered
Alberto LeroyPosted Apr 22, 2020, 2:31 PM
Very useful! Little typo in the name of the iOS section (just to reach perfection!!!)
Divya RPosted Apr 2, 2020, 7:47 AM
S.Ravi Kumar Thanks for such good article!!
Clifton SteenkampPosted Sep 30, 2019, 5:13 AM
I've found a solution, created a pull request on the sample project's github repository.
Clifton SteenkampPosted Sep 30, 2019, 5:13 AM
Hi there RaviThanks for the wonderful article, it was really helpful. While testing your ShowHidePassword solution on android though, I noticed that the blinker in the Entry jumps from the start of the entry to the end every time I either show or hide the password. ? I noticed this problem is also in the image shown in your article. Is there any way to resolve this issue?
Oliver HütterPosted Apr 29, 2019, 3:43 AM
How to make the icon a bit bigger?
Allison TaofeekPosted Dec 25, 2018, 12:54 AM
I want the eye button on the right on ios
Allison TaofeekPosted Dec 25, 2018, 12:53 AM
I want the eye button to be on the right
RudolfPosted Nov 7, 2018, 3:24 AM
On Adnroid use this if (e.GetX() >= (editText.Right - editText.PaddingRight - editText.GetCompoundDrawables()[2].Bounds.Width())) in ShowHidePassEffect.cs
yannick kempPosted Nov 23, 2017, 6:09 AM
Thank for this code , but in certain android device(P9) the keyboard don't display