To create a TextInputLayout with a password field that includes a toggle for showing and hiding the password, you can use the passwordToggleEnabled attribute in XML. Here's how to implement it step-by-step:
Make sure you have the Material Components library included in your build.gradle file:
implementation 'com.google.android.material:material:1.6.0'
In your XML layout file, use TextInputLayout with passwordToggleEnabled set to true:
res/layout/activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" app:hintEnabled="true" android:hint="Password"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
You typically don't need to handle the toggle programmatically, as it's handled automatically by TextInputLayout. However, if you want to customize it further, you can find the toggle button and manage its behavior.
Here's an example of how you might set this up in an activity:
import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; public class MainActivity extends AppCompatActivity { private TextInputLayout textInputLayout; private TextInputEditText editTextPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textInputLayout = findViewById(R.id.textInputLayout); editTextPassword = findViewById(R.id.editTextPassword); } } By setting passwordToggleEnabled to true in your TextInputLayout, you allow users to easily toggle the visibility of their password. This enhances user experience by providing a convenient way to verify their input.
How to enable password visibility toggle in TextInputLayout?
passwordToggleEnabled attribute to allow users to toggle password visibility.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout>
How to customize the password toggle icon in TextInputLayout?
app:passwordToggleDrawable.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" app:passwordToggleDrawable="@drawable/custom_toggle_icon"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout>
How to handle password visibility toggle programmatically?
val passwordInputLayout = findViewById<TextInputLayout>(R.id.passwordInputLayout) passwordInputLayout.isPasswordVisibilityToggleEnabled = true
How to set hint for TextInputLayout with password toggle?
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your password" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout>
How to validate password input with TextInputLayout?
val passwordInputLayout = findViewById<TextInputLayout>(R.id.passwordInputLayout) passwordInputLayout.setEndIconOnClickListener { val password = passwordInputLayout.editText?.text.toString() if (password.length < 6) { passwordInputLayout.error = "Password too short" } else { passwordInputLayout.isErrorEnabled = false } } How to change the password toggle visibility state?
passwordInputLayout.setEndIconOnClickListener { val currentType = passwordInputLayout.editText?.inputType passwordInputLayout.editText?.inputType = if (currentType == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD } else { InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD } passwordInputLayout.editText?.setSelection(passwordInputLayout.editText?.text?.length ?: 0) } How to customize error messages in TextInputLayout with password toggle?
if (password.isEmpty()) { passwordInputLayout.error = "Password cannot be empty" } How to programmatically change password toggle enabled state?
passwordInputLayout.isPasswordVisibilityToggleEnabled = false // Disable toggle
How to handle password visibility toggle in a Fragment?
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val passwordInputLayout = view.findViewById<TextInputLayout>(R.id.passwordInputLayout) passwordInputLayout.isPasswordVisibilityToggleEnabled = true } How to reset password field in TextInputLayout with toggle?
passwordInputLayout.editText?.setText("") passwordInputLayout.isPasswordVisibilityToggleEnabled = true // Reset state yolo dynamicobject spock mplcursors space-analysis closedxml windows-phone background-task adapter ora-00933