Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Compose Jetpack Autofocus on TextField when screen open or programmatically on application requirement

I am using android compose jetpack version "0.1.0-dev14". According to application requirement, I want to auto focus TextField once screen is visible. and another scenario is like I want to focus next TextField in same screen on next ImeAction.Next action. I could not find any method or solution for this problem. If Anyone can help me to sole this problem, It will be really appreciated.

Thank you..!!

like image 271
Madhav Gor Avatar asked Jul 17 '20 06:07

Madhav Gor


People also ask

What is Jetpack compose in Android?

Jetpack Compose Jetpack Compose is Android’s modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.

What is textfield in 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.

What is compose for Android?

It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs. Do more with less code and avoid entire classes of bugs, so code is simple and easy to maintain. Just describe your UI, and Compose takes care of the rest.

What are textfield and textfield in compose?

At a higher level, Compose provides Text and TextField , which are composables following Material Design guidelines. It’s recommended to use them as they have the right look and feel for users on Android, and includes other options to simplify their customization without having to write a lot of code.


1 Answers

With 1.0.0 to achieve autofocus you can use:

DisposableEffect(Unit) {
    focusRequester.requestFocus()
    onDispose { }
}

In this way the system grants focus to the component associated with this FocusRequester. For example:

val focusRequester = FocusRequester()
TextField(
    //...
    modifier = Modifier
        // add focusRequester modifier 
        .focusRequester(focusRequester)
)
like image 128
Gabriele Mariotti Avatar answered Sep 27 '22 18:09

Gabriele Mariotti