Skip to content
Related Articles
Open in App
Not now

Related Articles

Toggle Password Field in Android using Jetpack Compose

Improve Article
Save Article
  • Last Updated : 30 Apr, 2022
Improve Article
Save Article

In the Login or Sign-up-based application, you must have noticed that the Password field displays black filled circled upon typing the password requirement. Additionally, there is an eye-shaped icon, which on click displays the password in the form of text. This icon acts as a toggle, as it helps the user to display the typed password in case the user is unsure about it.

Toggle Password Field


So in this article, we will show you how you could TextField with toggling password in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Dependency build.gradle (Module)

Add the following dependency for importing the icons library from Material UI.


dependencies {
    implementation "androidx.compose.material:material-icons-extended:$compose_version"

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package com.geeksforgeeks.passwordtoggle
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            // Creating a Simple Scaffold 
            // Layout for the application
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Toggle Password", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
                // Creating Content
                content = {
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
                        // Creating a variable to store password
                        var password by remember { mutableStateOf("") }
                        // Creating a variable to store toggle state
                        var passwordVisible by remember { mutableStateOf(false) }
                        // Create a Text Field for giving password input
                            value = password,
                            onValueChange = { password = it },
                            label = { Text("Password") },
                            singleLine = true,
                            placeholder = { Text("Password") },
                            visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                            trailingIcon = {
                                val image = if (passwordVisible)
                                else Icons.Filled.VisibilityOff
                                // Localized description for accessibility services
                                val description = if (passwordVisible) "Hide password" else "Show password"
                                // Toggle button to hide or display password
                                IconButton(onClick = {passwordVisible = !passwordVisible}){
                                    Icon(imageVector  = image, description)
Note: to use Icons.Filled.Visibility and Icons.Filled.VisibilityOff 
add in the dependencies: implementation "androidx.compose.material:material-icons-extended:$compose_version"


You can see that we are able to toggle between hiding and displaying the password.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!