How to switch between hide and view password
Solution 1
You can dynamically change the attributes of a TextView. If you would set the XML Atrribute android:password
to true the view would show dots if you set it to false the text is shown.
With the method setTransformationMethod you should be able to change this attributes from code. (Disclaimer: I have not tested if the method still works after the view is displayed. If you encounter problems with that leave me a comment for me to know.)
The full sample code would be
yourTextView.setTransformationMethod(new PasswordTransformationMethod());
to hide the password. To show the password you could set one of the existing transformation methods or implement an empty TransformationMethod that does nothing with the input text.
yourTextView.setTransformationMethod(new DoNothingTransformation());
Solution 2
It is really easy to achieve since the Support Library v24.2.0.
What you need to do is just:
-
Add the design library to your dependecies
dependencies { compile "com.android.support:design:24.2.0" }
-
Use
TextInputEditText
in conjunction withTextInputLayout
<android.support.design.widget.TextInputLayout android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" android:layout_marginBottom="@dimen/login_spacing_bottom"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/fragment_login_password_hint" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout>
The passwordToggleEnabled
attribute will do the job!
In your root layout don't forget to add
xmlns:app="http://schemas.android.com/apk/res-auto"
You can customize your password toggle by using:
app:passwordToggleDrawable
- Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint
- Icon to use for the password input visibility toggle.
app:passwordToggleTintMode
- Blending mode used to apply the background tint.
More details in TextInputLayout documentation.
For AndroidX
Replace
android.support.design.widget.TextInputLayout
withcom.google.android.material.textfield.TextInputLayout
Replace
android.support.design.widget.TextInputEditText
withcom.google.android.material.textfield.TextInputEditText
Solution 3
To show the dots instead of the password set the PasswordTransformationMethod:
yourEditText.setTransformationMethod(new PasswordTransformationMethod());
of course you can set this by default in your edittext element in the xml layout with
android:password
To re-show the readable password, just pass null as transformation method:
yourEditText.setTransformationMethod(null);
Solution 4
To show:
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
To hide:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
After each of these the cursor is reset, so:
editText.setSelection(editText.length());
Solution 5
You can use app:passwordToggleEnabled="true"
here is example given below
<android.support.design.widget.TextInputLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:textColorHint="@color/colorhint"
android:textColor="@color/colortext">
Related videos on Youtube
jacknad
Electrical Engineer with experience in microprocessor hardware design, ASM, PL/M, C/C++, C#, Android, Linux, Python, and Java. First high school radio design burst into flames during the demo. First software program was FORTRAN on punch cards. Worked in FL, IL, ND, NJ, TX, VA, and WA.
Updated on May 30, 2021Comments
-
jacknad almost 3 years
Is there a clever way to let the user switch between hide and view password in an android EditText? A number of PC based apps let the user do this.
-
Wilka almost 13 years
android:password
is deprecated now, and you should useandroid:inputType
instead. -
Mostafa over 12 yearsYou can also make your users happy by saving cursor position before setting transformation method and restoring after it. Use
editText.getSelectionStart()
andeditText.getSelectionEnd()
for saving cursor position andeditText.setSelection(start, end)
for restoring it. -
Johann almost 11 years@Wilka: android:inputType does NOT let you switch back and forth between the two states at run-time. It only allows you to switch once and once you change it, you cannot change it back. And no, the setTransformationMethod is NOT deprecated.
-
Matt Quigley over 9 yearsTo show the password, you don't need to make any new classes. Just call
setTransformationMethod(null)
. -
sujith s about 9 years@Janusz , use of the following will give gud solution. setTransformationMethod(PasswordTransformationMethod.getInstance()); and setTransformationMethod(HideReturnsTransformationMethod.getInstance());
-
Narendra Singh over 8 years@Janusz but how to get show / hide keys in keyboard?
-
tsiro about 8 yearswhen calling setTransformationMethod on an EditeText the onTextChanged callback of the EditText is being called...is it possible not this being happened?
-
guisantogui about 8 yearsDo you have any idea how to use setError with this component, once the error is flagged the show/hide icon become invisible
-
Sergio over 7 yearsCorrect. The "setTransformationMethod() is the key. All you need is switch in your listener: etPassword.setTransformationMethod(null) / etPassword.setTransformationMethod(new PasswordTransformationMethod()). By default, set in your xml EditView "android:inputType="textPassword""
-
MiguelHincapieC over 7 yearsIn version 25.1.0 I have a weird behavior: it shows the password toggle once but if you press it, it will disappear o_O'
-
mmBs over 7 yearsYeah, there are also some quirks with
android:text
attribute onTextInputEditText
. You can always raise an issue on Google Issues Tracker for Android -
Samad over 7 yearsis there any way to move the icon to the left side?
-
mmBs over 7 years@Delta7 There are some ways and workarounds. Please, ask the question on SO, paste a link here and I will try to help you.
-
Gunavant Patel about 5 yearsNot need to set text again only call firstEditText.invalidate();
-
Ruslan Berozov about 5 yearsToday it must be a preferred to use answer
-
Sadda Hussain over 4 yearsThat increases the height of the view in my case. Is there any way to remove padding from toggle drawable.
-
FabioR over 4 yearsThe only way that actually worked for me programmatically, I needed my TextInputEditText to be numbers only and to also allow password visibility to be toggled on and off (when the person clicks the eye icon). These are the lines that helped me achieve that:
textnputLayout.isPasswordVisibilityToggleEnabled = true textInputEditText.transformationMethod = PasswordTransformationMethod.getInstance() textInputEditText.inputType = InputType.TYPE_CLASS_NUMBER
-
Nicolas about 4 yearsThis is deprecated in material components. Use
app:endIconMode="password_toggle"
. -
Josh Sutterfield almost 4 yearsThanks. However, in my build the show/hide graphics are oddly reversed from your screenshot - it shows the crossed-out-eye when password is hidden - I guess someone decided buttons should show current state instead of action (or goal state).
-
Rahmat Ihsan almost 4 years@JoshSutterfield agree. so if we want action button, we have to reverse it manually using
app:passwordToggleDrawable
(deprecated) orapp:endIconDrawable
then use custom drawable like<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_eye_close" android:state_checked="true"/> <item android:drawable="@drawable/ic_eye_open"/> </selector>
I think google should fix this behavior. Good discussion -
FluffyKitten over 3 yearsWelcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain how this answers the question and and how it improves on the many existing upvoted answers answers the question already has, so that it is useful to the OP as well as other users with similar issues.