11

Is there a way to register a method so that it's automatically called as soon as the user ends "text selection mode" (thereby copying selection to clipboard)?

Regex Rookie
  • 10,432
  • 15
  • 54
  • 88

3 Answers3

13

I think you're looking for ClipboardManager.addPrimaryClipChangedListener().

Edit - this is for Android 3.0, and I don't see any other clipboard-related apis in the earlier levels.

Also see the ClipboardManager docs in the copy and paste guide.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • 1
    I think you're right. :) Unfortunately, it's only available from API level 11. I need something that would work at least on Android 2.2 (API level 8). http://developer.android.com/guide/appendix/api-levels.html#level11 Nevertheless, your answer is the correct one and so I am going to accept it, although I would love to see other suggestions that somehow work on API level 8 (polling?) – Regex Rookie Mar 07 '11 at 19:42
2

I need the exact same functionality. For the moment I'm solving this with android.text.ClipboardManager and a java Timer that checks every second if the clipboard-text has changed.

Jules Colle
  • 11,227
  • 8
  • 60
  • 67
2

If you are using Android 3.0 you can add a listener with ClipboardManager.addPrimaryClipChangedListener().

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • It triggers the callback even if same text is copied again and again. Can you listen only when copied text is different than before? – Usman Rana Dec 24 '20 at 09:43