Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with deprecated android.text.ClipboardManager

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?

like image 430
Mike Crittenden Avatar asked Nov 08 '11 20:11

Mike Crittenden


People also ask

What does deprecated mean in Android?

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.

How do I use clipboard manager on Android?

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.

Which class should be used for placing and retrieving text to from the global 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.


2 Answers

I ended up just using the old way (android.text.ClipboardManager and the code from this answer), along with a couple @SuppressWarnings("deprecation") annotations.

like image 186
Mike Crittenden Avatar answered Oct 19 '22 06:10

Mike Crittenden


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");
}
like image 38
Beeing Jk Avatar answered Oct 19 '22 04:10

Beeing Jk