Search code examples
c#android.netxamarinandroid-edittext

Xamarin EditText InputType Password


I got some EditText fields that are created from a JSON file, so I cannot change the XML files. But I want some EditText fields to have input type "password". I'm working in Xamarin Studio with C#.

I got something like this but that doesn't work:

editText.InputType = InputTypes.TextVariationPassword;

Solution

  • Try this:

    editText.InputType = Android.Text.InputTypes.TextVariationPassword | 
                              Android.Text.InputTypes.ClassText;
    

    Edit: There are still some interest (i.e. votes) in this answer now after 2 years, so I thought of adding an explanation with the hope it will help others.

    In many other languages/platforms (e.g. HTML), you just set your control to password and it works. That's what the OP was trying to do, but Android is designed differently (and quite odd IMHO). It divides the flags to classes and variations. You have to choose a class first (e.g. ClassText) and then apply the variations you want (e.g. TextVariationPassword). If you only set the variation, Android does not know what class you want. For instance, you can have a text password TextVariationPassword (so you combine it with the text class ClassText), or you can have a numeric password NumberVariationPassword (so you combine it with the number class ClassNumber).

    Now all that explained starts to make some sense, except for a question somebody may ask: Can Android not simply infer the class from the variation so we don't have to specify redundant things? For the simple cases above it's pretty easy, but it gets more complex with several flags set to one control. Yet, I still personally think that it is relatively easily doable and better than the current redundant design that's taking many developers by surprise.