4

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")
    }
4
  • I think you can achieve that with just compose mutableStateOf holder and onValueChange lambda. Could you elaborate on your use case? Commented Jun 19, 2022 at 11:36
  • I want to replace my IME connection from 'InputMethodService.getCurrentInputConnection()' to my own TextField inside IME Commented Jun 19, 2022 at 11:49
  • 1
    hey, have u solved this? Commented Mar 5, 2024 at 3:41
  • 1
    @galihif I have found a workaround that you can see in this PR: github.com/florisboard/florisboard/pull/1897 Commented Mar 10, 2024 at 14:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How does this help in getting InputConnection?
0

Starting with Compose 1.7.0, you can use InterceptPlatformTextInput to get the InputConnection.

var inputConnection by remember { mutableStateOf<InputConnection?>(null) }

InterceptPlatformTextInput(
    interceptor = { request, nextHandler ->
        // if you want to disable the keyboard
        inputConnection = request.createInputConnection(EditorInfo())
        awaitCancellation()

        // if you want to keep the keyboard
        nextHandler.startInputMethod { outAttributes ->
            request.createInputConnection(outAttributes)
                .also { inputConnection = it }
        }
    },
    content = {
        TextField(...)
    },
)

Button(
    onClick = { inputConnection?.commitText("hello", 5) },
    content = { Text("test") },
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.