How to Clear Focus of TextField in Android using Jetpack Compose?
In Jetpack Compose, a TextField is a UI element that lets users type in text as an input. This input can then be stored and used for various desired functions. When a user clicks the TextField, a soft keyboard pops up from the bottom and the user can type in the desired text. The TextField cursor keeps blinking unless the back button is clicked or the screen is clicked outside the TextField. This is called clearing focus from TextField.
So in this article, we will show you how you could programmatically clear the focus of TextField 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.jcclearfocus import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.material.* import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Creating a Simple Scaffold // Layout for the application Scaffold( // Creating a Top Bar topBar = { TopAppBar(title = { Text( "GFG | Clear Focus BasicTextField" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, // Creating Content content = { // Creating a Column Layout Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { // Creating a values and variables to remember // focus requester, manager and state val focusRequester = remember { FocusRequester() } val focusManager = LocalFocusManager.current var value by remember { mutableStateOf( "" ) } // Creating a Basic TextField BasicTextField( value = value, onValueChange = { value = it }, decorationBox = { innerTextField -> Row( Modifier .background(Color.LightGray, RoundedCornerShape(percent = 30 )) .padding( 16 .dp) .focusRequester(focusRequester) ) { //... innerTextField() } } ) // Adding a Space of height 100dp Spacer(Modifier.height( 100 .dp)) // Creating a Button that clears // focus of the above TextField Button(onClick = { focusManager.clearFocus() }, colors = ButtonDefaults.buttonColors(backgroundColor = Color( 0XFF0F9D58 ))) { Text( "Clear focus" , color = Color.White) } } } ) } } } |
Output:
You can see that when the button is clicked, the cursor disappears indicating that the focus is cleared from the TextField.