In my application, I have an EditText
whose default input type is set to android:inputType="textPassword"
by deault. It has a CheckBox
to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that is
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
My problem is, when that CheckBox is unchecked it should again set the input type to PASSWORD. I've done it using-
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
But, the text inside that edittext is still visible. And for surprise, when I change the orienatation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).
Any way to achieve this?
Since the Support Library v24.2.0. you can achivie this very easy
What you need to do is just:
Add the design library to your dependecies
Use
TextInputEditText
in conjunction withTextInputLayout
passwordToggleEnabled
attribute will make the password toggle appearIn your root layout don't forget to add
xmlns:app="http://schemas.android.com/apk/res-auto"
You can customize your password toggle by using:
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.![enter image description here](https://i.stack.imgur.com/odm0r.png)
The Password Visibility Toggle feature has been added to support library version 24.2.0 enabling you to toggle the password straight from the
EditText
without the need for aCheckBox
.You can make that work basically by first updating your support library version to 24.2.0 and then setting an
inputType
of password on theTextInputEditText
. Here's how to do that:You can get more information about the new feature on the developer documentation for TextInputLayout.
I would remove
android:inputType="textPassword"
from your layout. That is why it is switching back to password when the orientation changes. Because each time the orientation changes the view is being recreated.As for the first problem try this:
basically emptying out the text before you change the input type and then add it back.
Based on answers of neeraj t and Everton Fernandes Rosario I wrote in Kotlin, where
password
is an id of an EditText in your layout.Ok So after hours of trying finally implemented it. Below is the code ..
Explanations:- I have a button with default text as show. After onclick event on it checking if button's text is show. If it is show then changing the input type,adjusting the cursor position and setting new text as hide in it.
When it is hide... doing reverse i.e. hiding the password,adjusting the cursor and setting the text as show. And that's it. It is working like a charm.