How to display custom password character with some delay like EditText control

EswaranEswaran INMember ✭✭

I have created a custom control by inheriting the EditText control. My requirements are to display the custom password character and when typing there should be some delay like EditText when setting the input type. I have achieved the custom password character displaying but I am unable to achieve the second requirement. I tried to achieve the second requirement by using PostDelay. When typing a single character I can have some delay before it changes to password character but when typing multiple characters fastly I cant able to achieve the requirement similar to edit text control. I have attached the sample in the below link: https://github.com/Eswaran17392/CustomSamples/blob/master/CustomEditbox.zip. Anyone, please suggest me how to achieve the delay similar to EditText. For applying delay, I used below code snippet:

if(ShowPasswordDelay)
{
Java.Lang.Runnable runnable = new Java.Lang.Runnable(() =>
{
this.Text = value;
});
if (handler != null)
{
handler.RemoveCallbacks(runnable);
handler = null;
this.Text = value;
}

if (handler == null)
handler = new Handler();
handler.PostDelayed(runnable, 1000);
}
else
{
this.Text = value;
}

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited August 9

    @Eswaran

    Why you want to custom this Edittext, Android have been designed it for password in EditText like following code. You can set the InputTypes for that.

     EditText editText1 = FindViewById<EditText>(Resource.Id.editText1);
            //InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
     editText1.InputType = Android.Text.InputTypes.TextVariationPassword |
                           Android.Text.InputTypes.ClassText;
    

    Here is running GIF.

  • EswaranEswaran INMember ✭✭

    Hi @LeonLu ,

    When using the below code
    editText1.InputType = Android.Text.InputTypes.TextVariationPassword |
    Android.Text.InputTypes.ClassText;

    All the characters will be displayed as a dot character. But my first requirement is to display the custom password character like the below-attached image. I can't use the above code because when using the password input type, all my custom password character will be changed to a dot character by default. As per my first requirement, my output should be like the below image:

    To achieve the first requirement, I will convert all the typed character to my own custom character. My second requirement is to have some delay when typing the character so that it shows the typed character for one second and after one second it should be changed to my custom password character. I have done this in the xamarin.iOS platform using UITextfield control. Please have the demo video link: https://drive.google.com/open?id=1xtfuBSPQ_RImK5KRrAoAxfBEnWVcI3Z9

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited August 9

    @Eswaran You can set the edit.TransformationMethod for your edittext to set the * for your password like this thread.
    https://stackoverflow.com/questions/57423074/how-to-display-custom-password-character-with-some-delay-like-edittext-control

    EditText editText1 = FindViewById(Resource.Id.editText1);

            editText1.TransformationMethod = new CustomPasswordTransformationMethod();
    

    CustomPasswordTransformationMethod

     class CustomPasswordTransformationMethod : PasswordTransformationMethod
        {
    
            public override ICharSequence GetTransformationFormatted(ICharSequence source, View view) {
    
                return new PasswordCharSequence(source);
            }
        }
    

    PasswordCharSequence

             class PasswordCharSequence : Java.Lang.Object, ICharSequence
        {
            private ICharSequence mSource;
    
            public PasswordCharSequence(ICharSequence mSource)
            {
                this.mSource = mSource;
            }
            public char CharAt(int index)
            {
                return '*'; // here is your custom password character
            }
            public IEnumerator<char> GetEnumerator()
            {
                return mSource.GetEnumerator();
            }
            public int Length()
            {
                return mSource.Length();
            }
    
            public ICharSequence SubSequenceFormatted(int start, int end)
            {
                return mSource.SubSequenceFormatted(start, end);
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return mSource.GetEnumerator();
            }
        }
    

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Eswaran Are there any updates for this issue? If the reply is helpful, please mark it as an answer, it will help others who have similar issue.

  • EswaranEswaran INMember ✭✭

    Hi @LeonLu ,

    Sorry for the delay. I cannot implement your solution in my case because I will display some literal characters along with the password characters. Like in the below image

    In the above image '*' is the password character, '\' is the literal character and '_' is my prompt character. When typing the text the prompt character will be converted to the password character and the literal character will remain the same as in the below image.

Sign In or Register to comment.