Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve deprecated ClipboardManager in Jetpack Compose?

The ClipboardManager interface was deprecated. What should I change?

like image 773
Jiří Avatar asked Jan 22 '26 09:01

Jiří


2 Answers

The ClipboardManager interface was deprecated in favor of Clipboard interface.

  1. Change all import androidx.compose.ui.platform.LocalClipboardManager to import androidx.compose.ui.platform.LocalClipboard and LocalClipboardManager to LocalClipboard

  2. Make the caller function suspend or use rememberCoroutineScope().launch { ... }

  3. If you were using function setText(AnnotatedString(text)), then you can replace it with setClipEntry(ClipEntry(ClipData.newPlainText(text, text)))

According to the Jetpack Compose tests, the replacement for hasText() is getClipEntry()?.clipData?.description?.hasMimeType("text/*")

like image 197
Jiří Avatar answered Jan 24 '26 00:01

Jiří


For KMP Projects try this:

Android:

val localClipboard = LocalClipboard.current
val scope = rememberCoroutineScope()
val clipData = ClipData.newPlainText("label", "text")
scope.launch{localClipboard.setClipEntry(clipData.toClipEntry())}

IOS:

val localClipboard = LocalClipboard.current
val scope = rememberCoroutineScope()
scope.launch{localClipboard.setClipEntry(ClipEntry.withPlainText("text"))}

I haven't tested it myself yet, but Android Studio doesn't swear at syntax. As soon as I test it, I will add an answer.

like image 30
Amin Avatar answered Jan 24 '26 00:01

Amin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!