Olson CloudWorks 🚀

Programmatically change input type of the EditText from PASSWORD to NORMAL vice versa

September 19, 2026

Programmatically change input type of the EditText from PASSWORD to NORMAL  vice versa

In Android development, user experience is paramount, and handling sensitive information like passwords requires careful consideration. One common requirement is the ability to toggle the visibility of password characters within an EditText field. This involves programmatically changing the input type of the EditText from PASSWORD to NORMAL and vice versa. This article will guide you through the process, providing a step-by-step approach and best practices to ensure a secure and user-friendly implementation. We will explore how to achieve this using Android’s built-in functionalities, focusing on code clarity and maintainability. Developers often need to provide a “show password” checkbox or button, allowing users to verify what they’ve typed before submitting potentially sensitive data. This dynamic adjustment of input types enhances usability and reduces the risk of incorrect password entries.

Understanding Input Types and EditText

The EditText widget in Android is a fundamental UI element for text input. It supports various input types, each tailored for specific data formats. The inputType attribute determines how the keyboard behaves and what type of characters are allowed. Two crucial input types for password handling are TYPE_TEXT_VARIATION_PASSWORD (which masks the input) and TYPE_TEXT_VARIATION_VISIBLE_PASSWORD (which displays the input as plain text). Programmatically switching between these types allows for a dynamic password visibility toggle. It’s important to note that these types can be combined with other input type flags, such as TYPE_CLASS_TEXT, to define the overall behavior of the EditText field. For example, setting inputType to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD configures the EditText for password input while masking the characters. Conversely, TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD will display the text normally.

Using the correct input types is critical for both security and user experience. Displaying a password in plain text without a user’s explicit action is a security risk. However, forcing users to repeatedly re-enter passwords due to typos is frustrating. Providing a controlled mechanism to reveal the password strikes a balance. According to a study by Baymard Institute, a clear “show password” option can reduce checkout abandonment rates by up to 20% [^1^][Baymard Institute]. This highlights the importance of this seemingly small feature in user interface design. The EditText widget is the foundation, but the intelligent use of input types is what makes it secure and user-friendly.

To effectively manage input types programmatically, understanding the underlying Android API is essential. The setInputType() method of the EditText class is the primary tool for modifying the input type at runtime. This method takes an integer representing the desired input type, which can be constructed using the constants defined in the InputType class. Correctly using this method, along with appropriate event handling (e.g., a button click or checkbox toggle), allows developers to seamlessly switch between password masking and visibility. This is what makes it possible to programmatically change the input type of the EditText from PASSWORD to NORMAL and vice versa.

Implementing the Password Visibility Toggle

Implementing a password visibility toggle involves several steps. First, you need to define the EditText and a toggle control (e.g., a CheckBox or ImageButton) in your layout XML file. Then, in your Activity or Fragment, you need to obtain references to these views using findViewById(). Next, you need to set an event listener on the toggle control to detect when the user interacts with it. Inside the event listener, you’ll use the setInputType() method to change the EditText’s input type based on the current state of the toggle. Finally, you should consider preserving the cursor position when switching input types to maintain a smooth user experience. Let’s look at specific examples.

To illustrate, consider the following code snippet (Java):

EditText passwordEditText = findViewById(R.id.passwordEditText); CheckBox showPasswordCheckBox = findViewById(R.id.showPasswordCheckBox); showPasswordCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int cursorPosition = passwordEditText.getSelectionStart(); if (isChecked) { passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } else { passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } passwordEditText.setSelection(cursorPosition); } }); 

This code snippet demonstrates the core logic for toggling password visibility. It retrieves references to the EditText and CheckBox, sets a listener on the CheckBox, and within the listener, changes the EditText’s input type based on the CheckBox’s state. The setSelection(cursorPosition) call is crucial for preserving the cursor position, preventing the cursor from jumping to the beginning of the text field when the input type changes.

Best Practices for Secure Password Handling

When implementing password handling, security should always be a top priority. Avoid storing passwords in plain text. Instead, use strong hashing algorithms like bcrypt or Argon2 to store password hashes. Always use HTTPS for transmitting sensitive data, and implement proper input validation to prevent injection attacks. Furthermore, consider implementing account lockout policies to prevent brute-force attacks. According to OWASP (Open Web Application Security Project), proper password storage and transmission are critical components of web application security [^2^][OWASP].

  • Always use strong hashing algorithms (bcrypt, Argon2).
  • Enforce password complexity requirements.
  • Implement account lockout policies.

Advanced Customization and Considerations

Beyond the basic implementation, there are several ways to customize the password visibility toggle. For instance, you can use an ImageButton instead of a CheckBox, providing a visual icon to indicate the password visibility state. You can also add animations to the icon transition for a more polished user experience. Additionally, you can implement custom input filters to restrict the characters that can be entered in the password field. For example, you might want to require a minimum password length or enforce the inclusion of special characters.

Another consideration is accessibility. Ensure that the toggle control is accessible to users with disabilities by providing appropriate content descriptions and keyboard navigation support. Use semantic HTML-like structures in your XML layouts to improve accessibility. For example, use android:contentDescription on your ImageButton to describe its purpose to screen readers. Proper accessibility ensures that all users can effectively use your application, regardless of their abilities.

Furthermore, consider the different keyboard layouts and input methods that users might employ. Test your implementation with various keyboard layouts to ensure that the password visibility toggle works correctly across different devices and configurations. Handling the IME (Input Method Editor) correctly is important for a consistent user experience. This might involve adjusting the layout or UI elements based on the IME’s visibility and size.

Infographic here
Troubleshooting Common Issues -----------------------------

Several common issues can arise when implementing a password visibility toggle. One common problem is the cursor jumping to the beginning of the EditText when the input type changes. This can be resolved by preserving the cursor position using setSelection(), as demonstrated in the code snippet earlier. Another issue is the keyboard disappearing when the input type changes. This can be addressed by manually requesting focus on the EditText after changing the input type using requestFocus() and showing the keyboard programmatically.

Another potential issue is incorrect input type flags. Ensure that you are using the correct combination of input type flags for password masking and visibility. For example, using only TYPE_TEXT_VARIATION_PASSWORD without TYPE_CLASS_TEXT will not produce the desired result. Carefully review the Android documentation for the InputType class to understand the different flags and their effects. Also, remember to handle configuration changes properly (e.g., screen rotation) to prevent the password visibility state from being reset. You can achieve this by saving and restoring the state of the toggle control in the onSaveInstanceState() and onRestoreInstanceState() methods.

Debugging these issues often involves using Android Studio’s debugging tools to inspect the EditText’s input type and cursor position at runtime. Use breakpoints and log statements to track the values of relevant variables and identify the source of the problem. Also, test your implementation on different devices and Android versions to ensure compatibility. Password management can be tricky, but with thorough testing and debugging, you can create a robust and user-friendly password visibility toggle.

  1. Define EditText and toggle control in XML.
  2. Get references to views in Activity/Fragment.
  3. Set an event listener on the toggle control.
  4. Change EditText’s input type using setInputType().
  5. Preserve cursor position using setSelection().

FAQ: Password Visibility Toggle

How do I programmatically change the input type of an EditText?
You can use the `setInputType()` method of the EditText class. Pass an integer representing the desired input type, using constants from the InputType class (e.g., `InputType.TYPE_TEXT_VARIATION_PASSWORD`).
What input types should I use for password masking and visibility?
For masking, use `InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD`. For visibility, use `InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD`.
How do I prevent the cursor from jumping when the input type changes?
Preserve the cursor position before changing the input type using `getSelectionStart()` and restore it afterwards using `setSelection()`.
How do I handle configuration changes (e.g., screen rotation)?
Save the state of the toggle control in `onSaveInstanceState()` and restore it in `onRestoreInstanceState()`.
Is it secure to store passwords in plain text in memory?
No, storing passwords in plain text is highly insecure. Always use strong hashing algorithms to store password hashes, even temporarily.
By understanding the nuances of `EditText` input types and implementing the password visibility toggle with care, you can significantly improve the user experience and security of your Android applications. The ability to programmatically change the input type of the `EditText` from PASSWORD to NORMAL and vice versa provides a valuable feature that allows users to confidently enter sensitive information. Remember to prioritize security best practices and accessibility considerations to ensure a robust and user-friendly implementation. For further information on Android UI development, refer to the official Android documentation \[^3^\]\[[Android Developers](https://developer.android.com/)\].
  • Prioritize user experience and security.
  • Use the correct input type flags.
  • Handle cursor position and keyboard behavior.

We’ve covered how to effectively implement a password visibility toggle in your Android applications, ensuring both usability and security. By dynamically adjusting the input type of the EditText, you empower users to verify their passwords before submission, reducing errors and frustration. Remember to always prioritize secure password handling practices and consider accessibility for all users. Why not explore related topics like implementing custom keyboard layouts or enhancing password strength validation in your next project? Your users will appreciate the attention to detail and commitment to a secure and user-friendly experience.

Question & Answer :
In my application, I have an EditText whose default input type is set to android:inputType="textPassword" by default. 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 orientation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).

Any way to achieve this?

Add an extra attribute to that EditText programmatically and you are done:

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

For numeric password (pin):

password.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); 

Also, make sure that the cursor is at the end of the text in the EditText because when you change the input type the cursor will be automatically set to the starting point. So I suggest using the following code:

et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); et_password.setSelection(et_password.getText().length()); 

When using Data Binding, you can make use of the following code:

<data> <import type="android.text.InputType"/> . . . <EditText android:inputType='@{someViewModel.isMasked ? (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) : InputType.TYPE_CLASS_TEXT }' 

If using Kotlin:

password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD