30

I use an EditText to enter password. And a CheckBox to show password or not. Below function is the part:

public void ShowPassword() { if (cb.isChecked()) { password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } else { password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); } } 

When it checked, it show password. But when it not checked, it does show stars. How to modify it to show star while the cb is not checked?

2
  • You forgot to ask the question. Commented Feb 16, 2012 at 8:17
  • Look at my answer about what is 129.. Commented Feb 16, 2012 at 9:13

12 Answers 12

69

I don't know exactly the specifics, but this code should work:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(!isChecked) { password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); } else { password.setInputType(129); } } }); 

129 is the input type set when setting android:inputType="textPassword"

edit:

as mentioned in @user370305's answer, 129 is the value of the bitwise or operation when you do

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
Sign up to request clarification or add additional context in comments.

4 Comments

no idea either. probably a combination of the InputType ints, but I don't know which one. I came up with 129 by logging the InputType before changing anything.
1) this should hide back password when isChecked = true. Looks like opposite; 2) hides it the password back?
instead of 129 you can use (InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_CLASS_TEXT) because int TYPE_TEXT_VARIATION_PASSWORD = 128; int TYPE_CLASS_TEXT = 1;
@GeorgeTheodorakis You need to remove the String "VISIBLE_" from your comment to make it correct.
49

This is not an answer,

Answer already given and accepted..

I just want to clarify about 129

password.setInputType(129); 

is Actually,

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

'|' is not a pipe, it's a bitwise OR. It takes two binary numbers and if either of the bits of equal value are 1,

How this relates to the input type: Each of the InputTypes are actually just ints. TYPE_CLASS_TEXT is 1, and TYPE_TEXT_VARIATION_PASSWORD is 128 (or 10000000).

Perform a bitwise OR on them:

00000001 10000000 ------------ 10000001 which is 129. 

Try entering input.setInputType(129); instead, you'll see it'll work. :)

1 Comment

Thanks, that cleared my mind. It's just sad knowing that the sdk doesn't have a single constant for something you can actually set on the xml itself.
16

I think you are using the wrong function. I make that way and work perfectly:

passwordEditView = (EditText) rootView.findViewById(R.id.password); final CheckBox showPasswordCheckBox = (CheckBox) rootView.findViewById(R.id.checkbox); showPasswordCheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (showPasswordCheckBox.isChecked()){ passwordEditView.setTransformationMethod(null); }else{ passwordEditView.setTransformationMethod(new PasswordTransformationMethod()); } } }); 

3 Comments

haven't encountered PasswordTransformationMethod before. interesting. what else can it do?
Where you put null, another post says to use this: HideReturnsTransformationMethod.getInstance() - from: stackoverflow.com/a/19109453/2162226 . Your answer worked for me, but do you know if one is better than another? .. thanks
Thank you! This is the right answer. The accepted answer does not work in my tests. Please see this post about why setInputType is not the right one: stackoverflow.com/questions/2586301/…
10

This might help you mate

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // checkbox status is changed from uncheck to checked. if (!isChecked) { // show password editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); } else { // hide password editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } } }); 

1 Comment

Found this answer to be really helpful and looks like the proper way to achiever this. But wondering why does this have least downvote :(
3

The Password Visibility Toggle feature has been added to support library version 24.2.0 enabling you to toggle the password straight from the password field without the need for a CheckBox.

You can make that work basically by setting an inputType of password on the TextInputEditText. Here's how to do that:

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout> 

4 Comments

I discovered this myself when suddenly the toggle started appearing on my screens. Is there any way to turn this off?
Nevermind, I just saw your article on Medium where you explain the options
Link to the document?
3

use app:passwordToggleEnabled = true; available from Android support library version 24.2.0.

 <android.support.design.widget.TextInputLayout android:id="@+id/input_layout_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" app:passwordToggleEnabled="true" tools:visibility="visible" android:layout_marginBottom="@dimen/space_medium"> <android.support.design.widget.TextInputEditText android:id="@+id/input_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:imeOptions="actionDone" android:imeActionLabel="@string/btn_sign_in" android:hint="@string/hint_password" /> </android.support.design.widget.TextInputLayout> 

Comments

2

instead of visible password, can you try with TYPE_TEXT_VARIATION_NORMAL

public void ShowPassword() { password.setInputType((cb.isChecked()) ? InputType.TYPE_TEXT_VARIATION_NORMAL : InputType.TYPE_TEXT_VARIATION_PASSWORD; } 

1 Comment

Yes, there are the same result.
1

Call this method inside your OnCheckedChangedListener

 public static void toggleShowPassword(boolean show, EditText editText) { if (show) editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); else editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); editText.setSelection(editText.length()); } 

The EditText cursor resets its position after changing the InputType that's why we add the last line editText.setSelection(editText.length())

Comments

1

This will work -

public void ShowPassword() { if (cb.isChecked()) { password.setInputType(InputType.TYPE_CLASS_TEXT); } else { password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } } 

Comments

0

on the off chance that you are using Xamarin (Visual Studio Mac as it's now called) you can achieve it this way (I used a Switch)

 /// <summary> /// Toggles the password. /// </summary> /// <param name="field">Field.</param> /// <param name="isChecked">If set to <c>true</c> is checked.</param> private void TogglePassword(TextView field, bool isChecked) { /// masks with password character if (isChecked) { field.TransformationMethod = new PasswordTransformationMethod(); } /// unmasks password else { field.TransformationMethod = null; } 

Then on your Switch .click do something like this

switch.Click += delegate { TogglePassword(textView, switch.Checked); }; 

Comments

-1

okay:

To show readeable text:

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) 

To "star" the password:

setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD) 

But why? The Type_Text_Variation_Password set it to stars, but why does only adding Type_Claas_Text revive the visibel password?
I would have thought that Type_Text_Variation_VISIBLE_Password comes into play ...

1 Comment

TYPE_TEXT_VARIATION_PASSWORD also lets the keyboard know that it should treat the user input as a password and hence not store it as an autocomplete suggestion.
-1
public void onCheckBox(View v2) { CheckBox cb = (CheckBox)this.findViewById(R.id.pass_Check); EditText et1=(EditText)this.findViewById(R.id.edt_Pass); if(cb.isChecked()) { et1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } else { et1.setInputType(129); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.