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