kotlin copy text android 10

To copy text in Kotlin for Android 10, you can follow these steps:

  1. First, create a function to handle the copy action:
fun copyToClipboard(text: CharSequence, label: String) {
    val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val clipData = ClipData.newPlainText(label, text)
    clipboardManager.setPrimaryClip(clipData)
}
  1. Then, call this function when the copy action is triggered, for example, when a button is clicked:
val textToCopy = "This is the text to be copied"
val label = "copied_text"
copyToClipboard(textToCopy, label)
  1. Ensure that you have the necessary permissions in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />

By following these steps, you can implement text copying functionality in Kotlin for Android 10.