Android: How to set password property in an edit text?
Asked Answered
W

9

51

I need to create a login form with 'username' 'password' fields and two buttons 'login' and 'cancel' in my android application.

I am using an alert dialog with edittext inside that.

This is the code I used to create password edittext..

     final EditText Password = new EditText(this);
     Password.setGravity(Gravity.CENTER);
     Password.setHint("Password");
     Password.setWidth(200);

     Password.setTransformationMethod(new PasswordTransformationMethod());
     login_alert.addView(Password);

My issue is that, plain text is shown instead of 'dots' when i open a softkeypad to edit the password. (It is shown as dots when not in softkeypad mode)

Can anyone suggest a solution?

Walsh answered 23/5, 2011 at 8:56 Comment(2)
Your member naming style will get you in trouble. What if Password is a real existing class, god beware one with static methods.Tutto
I tried renaming.. still no luckWalsh
S
98
Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

This one works for me.
But you have to look at Octavian Damiean's comment, he's right.

Smear answered 23/5, 2011 at 9:19 Comment(4)
Thanks a lot ernazm.. This one works fine. I have on more qn.. My "Password.setHint("Password");" seems not working now. Any idea?Walsh
Strange. Just tried to combine setHint with setInputType and it worked for me just fine. That's my code: EditText Password = ((EditText) findViewById(R.id.editText1)); Password.setHint("Password"); Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);Smear
Here is some explanation why you need both TYPE_CLASS_TEXT and TYPE_TEXT_VARIATION_PASSWORD.Obie
in case of Kotlin you should write "password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD"Oat
R
29

This is deprecated

In xml of EditText iclude this attribute: android:password="true"

Edit

android:inputType="textPassword"
Relativize answered 23/5, 2011 at 9:6 Comment(1)
android:password="true" is deprecatedYeeyegg
I
13

Here's a new way of putting dots in password

<EditText
    android:id="@+id/loginPassword"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="@string/pwprompt" /

add android:inputType = "textPassword"

Incrocci answered 17/2, 2015 at 6:11 Comment(1)
It seems to be the recommended way for xml declaration. developer.android.com/training/keyboard-input/style.htmlAcanthus
S
6

You need to use PasswordTransformationMethod.getInstance() instead of new PasswordTransformationMethod().

Sunbonnet answered 10/7, 2012 at 3:8 Comment(0)
S
4

The only way that worked for me using code (not XML) is this one:

etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
Swainson answered 4/11, 2015 at 8:36 Comment(0)
P
3

See this link text view android:password

This applies for EditText as well, as it is a known direct subclass of TextView.

Pinchpenny answered 23/5, 2011 at 9:0 Comment(2)
@PareshMayani, I'm sorry, my friend, but EditText is a subclass of TextView. Now, please stop "-1"-ing everything you are not aware of or you think is wrongPinchpenny
@PareshMayani and what's with the comment? You might as well improved my answer if you felt it wasn't straightforwardPinchpenny
T
3

I found when doing this that in order to set the gravity to center, and still have your password hint show when using inputType, the android:gravity="Center" must be at the end of your XML line.

<EditText android:textColor="#000000" android:id="@+id/editText2" 
    android:layout_width="fill_parent" android:hint="Password" 
    android:background="@drawable/rounded_corner" 
    android:layout_height="fill_parent" 
    android:nextFocusDown="@+id/imageButton1" 
    android:nextFocusRight="@+id/imageButton1" 
    android:nextFocusLeft="@+id/editText1"
    android:nextFocusUp="@+id/editText1" 
    android:inputType="textVisiblePassword" 
    android:textColorHint="#999999" 
    android:textSize="16dp" 
    android:gravity="center">
</EditText>
Troat answered 29/9, 2011 at 21:8 Comment(0)
F
3

To set password enabled in EditText, We will have to set an "inputType" attribute in xml file.If we are using only EditText then we will have set input type in EditText as given in below code.

                    <EditText
                    android:id="@+id/password_Edit"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="password"
                    android:imeOptions="actionNext"
                    android:inputType="textPassword"
                    android:maxLength="100"
                    android:nextFocusDown="@+id/next"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

Password enable attribute is

 android:inputType="textPassword"

But if we are implementing Password EditText with Material Design (With Design support library) then we will have write code as given bellow.

<android.support.design.widget.TextInputLayout
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/txtInput_currentPassword"
                android:layout_width="match_parent"
                app:passwordToggleEnabled="false"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password_Edit"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="@string/hint_currentpassword"
                    android:imeOptions="actionNext"
                    android:inputType="textPassword"
                    android:maxLength="100"
                    android:nextFocusDown="@+id/next"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </android.support.design.widget.TextInputLayout>

@Note: - In Android SDK 24 and above, "passwordToggleEnabled" is by default true. So if we have the customs handling of show/hide feature in the password EditText then we will have to set it false in code as given above in .

app:passwordToggleEnabled="true"

To add above line, we will have to add below line in root layout.

xmlns:app="http://schemas.android.com/apk/res-auto"
Flameproof answered 5/7, 2017 at 13:52 Comment(0)
H
1

My search for a similar solution for Visual Studio 2015/Xamarin lead me to this thread. While setting the EditText.InputType to Android.Text.InputTypes.TextVariationVisiblePassword properly hid the password during user entry, it did not 'hide' the password if it was visible before the EditText Layout was rendered (before the user submitted their password entry). In order to hide a visible password after the user submits their password and the EditText Layout is rendered, I used EditText.TransformationMethod = PasswordTransformationMethod.Instance as suggested by LuxuryMode.

Healthy answered 17/9, 2016 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.