android.text.ClipboardManager
was deprecated since API level 11, and replaced with android.content.ClipboardManager
(source).
How do I write code that supports both cases? Importing android.content.ClipboardManager
and using that works in 11+ but force closes in 10. Changing the import to android.text.ClipboardManager
throws a bunch of deprecation warnings in 11+.
How can I handle both cases smoothly? What do I need to import?
Deprecation means that we've ended official support for the APIs, but they will continue to remain available to developers. This page highlights some of the deprecations in this release of Android. To see other deprecations, refer to the API diff report.
Open the messaging app on your Android, and press the + symbol to the left of the text field. Select the keyboard icon. When the keyboard appears, select the > symbol at the top. Here, you can tap the clipboard icon to open the Android clipboard.
Here is an example demonstrating the use of ClipboardManager class. It creates a basic copy paste application that allows you to copy the text and then paste it via clipboard.
I ended up just using the old way (android.text.ClipboardManager and the code from this answer), along with a couple @SuppressWarnings("deprecation") annotations.
Referring to this answer:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
final android.content.ClipData clipData = android.content.ClipData
.newPlainText("text label", "text to clip");
clipboardManager.setPrimaryClip(clipData);
} else {
final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("text to clip");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With