Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get input connection from Jetpack Compose TextField?

I want to capture InputConnection of TextField on focus change, the method was onCreateInputConnection in EditText. Is there such method? How to achieve it without using AndroidView?

Tried this but doesn't work either.

@Composable
fun KeyboardScreen() {
    val rootView = LocalView.current
    ...
    Button(onClick = {
        val inputConnection = BaseInputConnection(rootView, true)
        inputConnection.commitText("hello", 5)
    }) {
        Text(text = "test")
    }
like image 714
Mohammad Hossein Kalantarian Avatar asked Jan 19 '26 07:01

Mohammad Hossein Kalantarian


1 Answers

That's nearly impossible at this point. I'm afraid you have to wrap the edit text. This is because LocalTextInputService is staticCompositionLocalOf and will cause all other composable to recompose and have the input service changed.

That would require you to write your own implementations of PlatformTextInputService and TextInputService class.

Then you need to provide the instance to the LocalTextInputService composition provider like:

class MyTextInputService : PlatformTextInputService {
   
}

class MyInputService : TextInputService(MyTextInputService()) {

}

@Composable
fun CustomInputServiceTextField(...){
    val inputService = remember { MyInputService() } 
    CompositionLocalProvider(LocalTextInputService provides inputService) {
        TextField(value = ..., onValueChange = ...)
    }
}

This is just an idea.

like image 121
Nikola Despotoski Avatar answered Jan 20 '26 22:01

Nikola Despotoski