'How to use clipboard service in jetpack compose

I want to copy a string to the user's mobile clipboard but I don't have any idea how I can use clipboard services in jetpack compose, If there is any alternative or any method that we can use to copy text to clipboard please share.



Solution 1:[1]

You can set and get text using LocalClipboardManager

val clipboardManager: ClipboardManager = LocalClipboardManager.current
var text by remember { mutableStateOf("")}

Column(modifier = Modifier.fillMaxSize()) {

    TextField(value = text, onValueChange = {text = it})
    Button(onClick = {
        clipboardManager.setText(AnnotatedString((text)))
    }) {
        Text("Copy")
    }

    Button(onClick = {
      clipboardManager.getText()?.text?.let {
          text = it
      }
    }) {
        Text("Get")
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thracian