I have an Edit text in android for a password field.The input type for the edit text is password.To, the right of the Edit text,I have a button which will show the password when the button is clicked.I have implemented the "Show Password" part using Motion Event in android.Below is the code which is working properly:
EditText passwordET;
Button showPassword;
passwordET = (EditText) findViewById(R.id.passwordET);
showPassword = (Button) findViewById(R.id.showpasswordBtn);
showPassword.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
passwordET.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
return true;
case MotionEvent.ACTION_UP:
passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
return true;
}
return true;
}
});
The above code is working fine for the case when the user clicks on the Button, the password is shown and as the button touch event is removed, the text becomes Password field again. But,what I want is that if the button is clicked once,the edit text will show the password and the password will remain shown until the button is clicked again.So, when the button is clicked again, the edit text will change its state to password type again.Can anyone please let me know how to proceed?