Copy and paste

Starting in Android 13, the system displays a standard visual confirmation when content is added to the clipboard. The new confirmation does the following:

  • Confirms the content was successfully copied.
  • Provides a preview of the copied content.

In Android 12L (API level 32) and lower, users are often unsure when they have successfully copied content or what they have copied.

This feature standardizes the various notifications shown by apps after copying and offers users more control over their clipboard.

Copy/Paste widget
Figure 1: New UI shown when content enters the clipboard.
Copy/Paste widget anatomy: preview, share, and send cross-device
Figure 2: Copy/Paste widget anatomy: preview, share, and send cross-device.

Avoid duplicate notifications

To avoid duplicate displays of information, we strongly recommend removing any pop-up notification shown after an in-app copy for Android 13 and higher.

Post snackbar after an in-app copy.
Figure 3: If you show a copy confirmation snackbar in Android 13, the user sees duplicate messages.
Post toast after an in-app copy.
Figure 4: If you show a copy confirmation toast in Android 13, the user sees duplicate messages.

Example

To avoid showing a duplicate copy confirmation UI in Android 13, try using code like this example:


  fun textCopyThenPost(textCopied:String) {
    val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
    // When setting the clip board text.
    clipboardManager.setPrimaryClip(ClipData.newPlainText("", textCopied))
    // Only show a toast for Android 12 and lower.
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2)
      Toast.makeText(context, “Copied”, Toast.LENGTH_SHORT).show()
  }

Notice that you don't need to include a pop-up message after copying the text to the clipboard for Android 13 and higher.

Add sensitive content to the clipboard

If your app allows users to copy sensitive content, such as passwords or credit card information, to the clipboard, you must add a flag to ClipData’s ClipDescription before calling ClipboardManager#setPrimaryClip(). Adding this flag prevents sensitive content from appearing in the content preview.

Copied text preview without flagging sensitive content
Figure 5: Copied text preview without flagging sensitive content.
Copied text preview flagging sensitive content.
Figure 6: Copied text preview flagging sensitive content.

To flag sensitive content, add a boolean extra to the ClipDescription. All apps should do this, regardless of the targeted API level.


// When your app targets API level 33 or higher
clipData.apply {
    description.extras = PersistableBundle().apply {
        putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)
    }
}

// If your app targets a lower API level
clipData.apply {
    description.extras = PersistableBundle().apply {
        putBoolean("android.content.extra.IS_SENSITIVE", true)
    }
}