copyTextToClipboard

fun Context.copyTextToClipboard(label: String, text: String, isSensitive: Boolean = false, onCopyFallback: () -> Unit = {}): Boolean(source)

Copies text to the clipboard.

  • Android 13+ shows system UI confirmation; avoid duplicate in-app snackbars there.

  • If isSensitive is true, the clipboard preview is obfuscated on Android 13+.

  • onCopyFallback is invoked only on API 32 and lower where in-app feedback is still needed.

The write is deliberately not read back to confirm it. ClipboardService allows OP_WRITE_CLIPBOARD without window focus ("Writing is allowed without focus.") but gates getPrimaryClip/getPrimaryClipDescription behind OP_READ_CLIPBOARD, which requires focus and returns null otherwise. A read-back therefore cannot tell a dropped write from a denied read, and would report a copy that succeeded while unfocused as a failure.

Return

true when the clipboard accepted the write, false when the service is missing or threw.