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?
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.
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.
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);
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With