0

My Android phone is paired to my Windows 11 PC. In it's Link to Windows is "sees" my desktop and states it is connected. In my Windows 11 PC settings - clipboard is set to sync and under Bluetooth is "sees" my phone. In my sample you will notice that I send an AnnotatedString. I sense that is my problem

 val clipboardManager = LocalClipboardManager.current
 val  output = "Hello World"
 clipboardManager.setText(AnnotatedString(output.toString()))

2 Answers2

0

Try this fun:

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.core.content.ContextCompat


fun copyToClipboard(context: Context, text: String) {
    val clipboard = ContextCompat.getSystemService(context, ClipboardManager::class.java)
    clipboard?.setPrimaryClip(ClipData.newPlainText("", text))
}

Like that

val context = LocalContext.current
copyToClipboard(context = context, text = "Text to copy")
Merkost
  • 1,251
  • 9
  • 16
  • it.setPrimaryClip does not compile. it has setText but does not accept (ClipData.newPlainText("", text)) – FoxHills MIcro Oct 18 '22 at 13:59
  • I have added implementations in my answer, please recheck if you are using them. I think you are using wrong ClipboardManager. – Merkost Oct 19 '22 at 00:00
  • The clipboard manager that I am using is Android JetPack Compose.... import androidx.compose.ui.platform.LocalClipboardManager – FoxHills MIcro Oct 19 '22 at 01:59
  • Glad for you! Try another one, which is android.content.ClipboardManager – Merkost Oct 20 '22 at 06:34
  • I changed my imports to agree with your sample. My code is contained within a @Composable function. I get two errors, "this' is not defined and my version of 'context' does not contain 'copyToClipboard'... Thanks for your help – FoxHills MIcro Oct 20 '22 at 13:01
  • The function "fun Context.copyToClipboard" should not be in Composable scope, place it in MainActivity, for example. – Merkost Oct 21 '22 at 01:06
  • I placed the code in non Composable class. The statement - val context = LocalContext.current, causes a compiler error, The "current" element is the problem. Error message - Composable invocations can only happen from the context of a @Composable function – FoxHills MIcro Oct 21 '22 at 22:43
  • Function copyToClipboard should be non composable and "val context = LocalContext.current context.copyToClipboard(text = "Text to copy")" - should be used inside a composable – Merkost Oct 22 '22 at 07:16
  • I moved the code to a Composable function. I created the appropriate import. However, 'context' does not contain a copyToClipboard(..) method. – FoxHills MIcro Oct 22 '22 at 14:28
  • Check the definition of the non-composable fun "fun Context.copyToClipboard(text: String)" and also imports for it "import android.content.ClipData import android.content.ClipboardManager import android.content.Context import androidx.core.content.ContextCompat" – Merkost Oct 23 '22 at 02:40
  • My prior post was not complete. The code in the non-composable function compiles with no problem! The code in the composable function is where the problem exists, Jetpack Compose's 'context' does not have: copyToClipboard – FoxHills MIcro Oct 23 '22 at 17:30
  • Check if this func started with Context. (should be like fun Context.copyToClipboard(text: String) and not private) – Merkost Oct 25 '22 at 04:58
  • I changed my function as follows: Context.copyToClipboard(text = "Text to copy") The compiler fails. It's OK with 'Contex', but does not like - copyToClipboard... – FoxHills MIcro Oct 25 '22 at 13:23
  • I also created a non Composable function which made the same call and get the same compiler error... – FoxHills MIcro Oct 25 '22 at 13:29
  • Have you added the rest of the function? {...}? – Merkost Oct 26 '22 at 00:53
  • This is the call from my non-Composable function that fails: Context.copyToClipboard(text = "Text to copy") – FoxHills MIcro Oct 26 '22 at 16:48
  • Your composable call should be "val context = LocalContext.current context.copyToClipboard(text = "Text to copy")" – Merkost Oct 27 '22 at 00:54
  • That code does not compile. "context" does not contain "copyToClipboard" . – FoxHills MIcro Oct 27 '22 at 12:24
  • I have changed the function in my answer. Please, try again with the new variant – Merkost Oct 27 '22 at 13:04
0
  @Composable
fun PasteToWindows(context:Context) {
    val message = remember {
        mutableStateOf("")
    }
    message.value = "Hello World Again & Again and"
    // Initializing the ClipboardManager and Clip data
    val clipboardManager =
        context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
    // clip data is initialized with the text variable declared above
    var clipData: ClipData = ClipData.newPlainText("text", message.value)
    clipboardManager.setPrimaryClip(clipData)
}

To copy from Windows clipboard, Add the "Swift Key" app to your Android phone. It enhances the Keypad with a clipboard property