How to Switch Between Hide and View Password

How to switch between hide and view password

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());

Show or Hide text using password toggle

Starting from the 1.2.0 this is the default behavior:

enter image description here
enter image description here

If you want to reverse the icon you can use something like:

    <com.google.android.material.textfield.TextInputLayout
app:endIconDrawable="@drawable/custom_password_eye"

with:

    <animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="NewApi">

<item
android:id="@+id/visible"
android:drawable="@drawable/design_ic_visibility"
android:state_checked="true"/>

<item
android:id="@+id/masked"
android:drawable="@drawable/design_ic_visibility_off"/>

<transition
android:drawable="@drawable/avd_show_password"
android:fromId="@id/masked"
android:toId="@id/visible"/>

<transition
android:drawable="@drawable/avd_hide_password"
android:fromId="@id/visible"
android:toId="@id/masked"/>

</animated-selector>

enter image description here
enter image description here

How to hide and show password on button click in android?

You have used OnTouchListener which gives you MotionEvent's. Use them! No need to check the button is pressed again as long as you are pressing it MotionEvent is there.

To visible a password field with with the password :inputType = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

When the button is pressed MotionEvent.ACTION_UP So you can visible the text.
When MotionEvent.ACTION_DOWN keep it as it was at the beginning.

 button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {

switch ( event.getAction() ) {

case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;

case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;

}
return true;
}
});

Edit text Password Toggle Android

(updated for AndroidX)

Since the Support Library v24.2.0. you can achivie this very easy

What you need to do is just:

  1. Add the design library to your dependecies

     dependencies {
    implementation "com.google.android.material:material:1.2.1"
    }
  2. Use TextInputEditText in conjunction with TextInputLayout

     <com.google.android.material.textfield.TextInputLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/etPasswordLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <android.support.design.widget.TextInputEditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password_hint"
    android:inputType="textPassword"/>
    </com.google.android.material.textfield.TextInputLayout>

passwordToggleEnabled attribute will make the password toggle appear


  1. In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto"

  2. 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.

enter image description here

How to switch between hide and view password

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());

How to switch between hide and view password

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());

How to switch between hide and view password in ios?

Simply setting

.secureTextEntry = YES

wont work I think due to a bug (or a feature), if the textField has focus.

Use something like this to make it work also if the textField is currently firstResponder

-(void) toggleTextFieldSecureEntry: (UITextField*) textField {
BOOL isFirstResponder = textField.isFirstResponder; //store whether textfield is firstResponder

if (isFirstResponder) [textField resignFirstResponder]; //resign first responder if needed, so that setting the attribute to YES works
textField.secureTextEntry = !textField.secureTextEntry; //change the secureText attribute to opposite
if (isFirstResponder) [self.textField becomeFirstResponder]; //give the field focus again, if it was first responder initially
}

Hide/show toggle inside password input

  1. First of all, make sure an id is only used once! (Second svg changed)
  2. You can hide and show the desired <svg> onclick
  3. Wrap the input and svg's into a container for styling
  4. I've moved the onclick to the <svg>

var x = document.getElementById("login-form-password");   // Input
var s = document.getElementById("Layer_1"); // Show pass
var h = document.getElementById("Layer_2"); // Hide pass

function togglePass() {
if (x.type === "password") {
x.type = 'text';
s.style.display = 'none';
h.style.display = 'inline';
} else {
x.type = 'password';
s.style.display = 'inline';
h.style.display = 'none';
}
}
#inputcontainer {
display: flex;
}
#inputcontainer > svg {
margin-left: 5px;
}
<p class="signin_title">Sign in</p>
<input type="text" id="login-form-username" name="os_username" placeholder="Username" required><br><br>

<div id='inputcontainer'>
<input type="password" id="login-form-password" name="os_password" placeholder="Password" required></input>

<svg id="Layer_1" onclick="togglePass()" data-name="Layer 1" width="25" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><title>eye-glyph</title><path d="M320,256a64,64,0,1,1-64-64A64.07,64.07,0,0,1,320,256Zm189.81,9.42C460.86,364.89,363.6,426.67,256,426.67S51.14,364.89,2.19,265.42a21.33,21.33,0,0,1,0-18.83C51.14,147.11,148.4,85.33,256,85.33s204.86,61.78,253.81,161.25A21.33,21.33,0,0,1,509.81,265.42ZM362.67,256A106.67,106.67,0,1,0,256,362.67,106.79,106.79,0,0,0,362.67,256Z"/></svg>
<svg id="Layer_2" onclick="togglePass()" data-name="Layer 2" width="25" xmlns="http://www.w3.org/2000/svg" style='display: none' viewBox="0 0 512 512"><title>eye-disabled-glyph</title><path d="M409.84,132.33l95.91-95.91A21.33,21.33,0,1,0,475.58,6.25L6.25,475.58a21.33,21.33,0,1,0,30.17,30.17L140.77,401.4A275.84,275.84,0,0,0,256,426.67c107.6,0,204.85-61.78,253.81-161.25a21.33,21.33,0,0,0,0-18.83A291,291,0,0,0,409.84,132.33ZM256,362.67a105.78,105.78,0,0,1-58.7-17.8l31.21-31.21A63.29,63.29,0,0,0,256,320a64.07,64.07,0,0,0,64-64,63.28,63.28,0,0,0-6.34-27.49l31.21-31.21A106.45,106.45,0,0,1,256,362.67ZM2.19,265.42a21.33,21.33,0,0,1,0-18.83C51.15,147.11,148.4,85.33,256,85.33a277,277,0,0,1,70.4,9.22l-55.88,55.88A105.9,105.9,0,0,0,150.44,270.52L67.88,353.08A295.2,295.2,0,0,1,2.19,265.42Z"/></svg>
</div>


Related Topics



Leave a reply



Submit