How to Validate TextFields in a Login Form in Android using Jetpack Compose?
In Android, TextField validation means checking if the user inputs are sufficient for a task to start or call a function. Generally, validation takes place in forms where the user is required to give mandatory inputs (fields marked with an asterisk). The most common examples of such forms are registration or sign-up forms and login forms.
In this article, we will show you how you could Validate TextFields in a Login Form 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: 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.
Kotlin
package com.w3wiki.jcvalidation import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Calling the composable function // to display element and its contents MainContent() } } } // Creating a composable // function to display Top Bar @Composable fun MainContent() { Scaffold( topBar = { TopAppBar(title = { Text( "GFG | Validation" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, content = { MyContent() } ) } // Creating a composable function // to create two TextFields // Calling this function as content // in the above function @Composable fun MyContent(){ // fetching local context val mContext = LocalContext.current Column( Modifier .fillMaxSize() .padding( 20 .dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { // Declaring two string values // for storing username and password val mUsername = remember { mutableStateOf( "" ) } val mPassword = remember { mutableStateOf( "" ) } // Creating two outlined text fields for // fetching username and password from the user OutlinedTextField( value = mUsername.value, onValueChange = { mUsername.value = it }, label = { Text(text = "Username" ) }, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = mPassword.value, onValueChange = { mPassword.value = it }, label = { Text(text = "Password" ) }, modifier = Modifier.fillMaxWidth() ) // Adding a Spacer Spacer(modifier = Modifier.height( 100 .dp)) // Button onclick would check // for the below conditions Button(onClick = { if (mUsername.value.isEmpty() and mPassword.value.isNotEmpty()){ Toast.makeText(mContext, "Username is Empty" , Toast.LENGTH_SHORT).show() } if (mPassword.value.isEmpty() and mUsername.value.isNotEmpty()){ Toast.makeText(mContext, "Password is Empty" , Toast.LENGTH_SHORT).show() } if (mUsername.value.isEmpty() and mPassword.value.isEmpty()){ Toast.makeText(mContext, "Username and Password are Empty" , Toast.LENGTH_SHORT).show() } if (mUsername.value.isNotEmpty() and mPassword.value.isNotEmpty()){ Toast.makeText(mContext, "Successfully Validated" , Toast.LENGTH_SHORT).show() } }, colors = ButtonDefaults.buttonColors(backgroundColor = Color( 0XFF0F9D58 )), ) { Text( "Next" , color = Color.White) } } } // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true ) @Composable fun DefaultPreview() { MainContent() } |
Output:
You can see that we are able to validate the TextFields using various conditions.