Mike Crittenden
Mike Crittenden

Reputation: 5917

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?

Upvotes: 15

Views: 12852

Answers (6)

Lawrence Gimenez
Lawrence Gimenez

Reputation: 3245

For those using Jetpack Compose

val clipboardManager = LocalClipboard.current
val clipData = ClipData.newPlainText("My Profile", AnnotatedString("Some string"))
clipboardManager.nativeClipboard.setPrimaryClip(clipData)                         

Upvotes: 0

Agilarasan anbu
Agilarasan anbu

Reputation: 2805

Updated code in kotlin without deprecated method.

Here my import :

import android.content.ClipboardManager
import android.content.Context
import android.content.ClipData

My text/button click

 bind.txtMail.setOnClickListener {
        copyToClipboard()
 }

Here is the fix bind.txtMail.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

private fun copyToClipboard() {
    val t = bind.txtMail.text.toString()
    val clipboardManager =
        bind.txtMail.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val clipData = ClipData.newPlainText(null, t)
    clipboardManager.setPrimaryClip(clipData)

    Toast.makeText(requireContext(), t, Toast.LENGTH_SHORT).show()
}

Upvotes: 0

Cristan
Cristan

Reputation: 14105

Reading the latest clipboard as text:

val clipboardManager = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
clipboardManager.addPrimaryClipChangedListener {
    val clipboardAsText = clipboardManager.primaryClip?.getItemAt(0)?.text
    if (clipboardAsText != null) {
        Toast.makeText(context, "Text in clipboard: $clipboardAsText", Toast.LENGTH_SHORT).show()
    }
}

Upvotes: 0

Beeing Jk
Beeing Jk

Reputation: 3926

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");
}

Upvotes: 7

Codeversed
Codeversed

Reputation: 9473

If you are still supporting < SDK 11 you are doing too much work. Set min to 15 and use this code:

 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText("label for text", "text to copy");
 clipboard.setPrimaryClip(clip);

Upvotes: 1

larham1
larham1

Reputation: 12236

Explicitly:

    @SuppressWarnings("deprecation")
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboard.setText(shareViaSMSBody);

Since this has to keep working on older devices, it is likely that the deprecated code will not be removed from Android.

Upvotes: 5

Related Questions