1

In Android, the way to get the clipboard text used to be simple:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
String text = clipboard.getText();

The getText() method has now been deprecated, and the documentation says to use getPrimaryClip() instead:

getText()

This method was deprecated in API level 11. Use getPrimaryClip() instead. This retrieves the primary clip and tries to coerce it to a string.

However, getPrimaryClip() returns a ClipData object, and it is not clear how to get the text content in this object. How can that be achieved?

Community
  • 1
  • 1
Chin
  • 19,717
  • 37
  • 107
  • 164

1 Answers1

0

Use this code:

public CharSequence getText() {
    ClipData clip = getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).coerceToText(mContext);
    }
    return null;
}

Source

Atef Hares
  • 4,715
  • 3
  • 29
  • 61