Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show the password with EditText

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?

like image 719
brian Avatar asked Feb 16 '12 08:02

brian


People also ask

How do I change my hide and view password?

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. To show the password, you don't need to make any new classes.

How do I change my toggle password in EditText?

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.


2 Answers

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); 
like image 95
josephus Avatar answered Oct 02 '22 23:10

josephus


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. :)

like image 41
user370305 Avatar answered Oct 02 '22 22:10

user370305