In my Android app there is a password editText than normally displays the dots instead of letters, under it there is a check box that is labeled Show Password. When the check box is checked for the first time the password displays but if unchecked it does not re-hide. I switched the state from hide at start to show at start and the first uncheck of the check box hides but then won't un-hide on subsequent clicks. The only code involved with this is:
public void chkShowKey_click ( View v ) {
if ( showKey.isChecked ( ) ) {
txtPassKey.setInputType ( 144 );
} else {
txtPassKey.setInputType ( 128 );
}
}
What is wrong? Is the isChecked()
not changing before the if statement checks the value?
When I try:
YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { //NOW USE THE BOOLEAN TO CHECK IF CHECKED } } );
Further testing with this code
chkShowKey.setOnCheckedChangeListener ( new CompoundButton.OnCheckedChangeListener ( ) {
@Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked ) {
if ( isChecked ) {
txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_NORMAL );
FancyToast.makeText(MainActivity.this,"Show",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
} else {
txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );
FancyToast.makeText(MainActivity.this,"Hide",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
}
//FancyToast.makeText(MainActivity.this,"Checkbox changed!",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
}
}
);
Shows that the event is firing and the isChecked
is changing, everything is working EXCEPT txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );
You can simply set an setOnCheckedChangeListener and Then check if Checked inside the Click Event and Deal with editText Accordingly
YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//NOW USE THE BOOLEAN TO CHECK IF CHECKED
}
}
);
If you r trying to show adn Hide password Follow below
To show or hide 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);