1

Background

We can use ClipboardManager to copy something to the clipboard, as such:

final ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
final ClipData clipData = ClipData.newPlainText("", someTextToCopy);
clipboard.setPrimaryClip(clipData);

The problem

Thing is, this code is silent, so the user doesn't see anything that shows that the text was copied.

I could add a toast of my own, but not all devices show a toast upon text copying by the user (G2 shows temporary notes window with the added content), and even for those that do, I can't find the string that they use, and also not the translations.

Here's how it looks like on LG G2 (wait till the copy is done):

https://youtu.be/cmmJC4_7EhE?t=33

And here's how a toast is shown on another device:

https://youtu.be/9S7ud5Ux5Fg?t=49

The question

Is there a way to invoke the default clipboard copying?

I'm talking about the one that's being used on EditText (or WebView, or anywhere you can copy text from as a user), when you mark text in it and choose "copy" .

Note that I don't use it for EditText/TextView. I want to invoke the default copying, even from a button.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • ""I could add a toast of my own, but not all devices show a toast (G2 shows temporary notes window with the added content), and even for those that do, I can't find the string that they use."" you mean toast is not showing in some devices? – GvSharma Feb 07 '16 at 11:09
  • @gvsharma I mean that when you, as a user mark text and then choose "copy", on some devices you see a toast, and on some (like G2) you see something else. – android developer Feb 07 '16 at 11:10
  • Possible duplicate of [How do I enable standard copy paste for a TextView in Android?](http://stackoverflow.com/questions/10386084/how-do-i-enable-standard-copy-paste-for-a-textview-in-android) – Gavriel Feb 07 '16 at 11:12
  • @Gavriel Please read the question again. I'm not talking about using it on EditText, and I already wrote that using a toast isn't a solution because not all devices show a toast on the default clipboard-copying. This is not a duplicate. – android developer Feb 07 '16 at 11:15
  • Let me quote from your question: "I'm talking about the one that's being used on EditText, when you mark text in it and choose 'copy'" If you prefer I can vote for closing it as "unclear what you want" – Gavriel Feb 07 '16 at 11:17
  • @Gavriel Please read the entire question. Not only a part of it. The sentence you talk about is what I want to achieve: use the exact same copying mechanism that's used on EditText on the device. This doesn't mean I want to use it on EditText. – android developer Feb 07 '16 at 11:19
  • Hmmm. I start to understand the question, but still hard to imagine what would the user experience be. Can you sketch a few images? I mean I see a button with the text Copy and/or icon like copy in the above video. How do I know what do I copy? Not the text "Copy" I suppose. Or if it's next to some textual content, then at the moment you click on copy I would "flash" the content similar to what it looks like do "select all" (with the blue background in the video" and possibly some vibration. But I don't see how the other menus (select all, cut to name 2) would apply – Gavriel Feb 07 '16 at 11:31
  • @Gavriel I've already shown an example of how it looks like on G2 about copying text using the default, built-in feature. What I ask is how to invoke it outside of the normal way the user does this (EditText text selection for example). This can be from anywhere, even from a notification. I don't need the other options and they wouldn't be relavant anyway. – android developer Feb 07 '16 at 11:37
  • So you only need a "copy" icon in some kind of overflow? In other words you DO NOT need the "default, built-in" feature, do you? If you don't then make your own solution. If you do, then explain to me what I asked in the previous comment. – Gavriel Feb 07 '16 at 11:48
  • @Gavriel I don't understand your question. I want to have the same UI result to the user as he copies from the clipboard from EditText (and others). The trigger is copying from somewhere else (notification, button, ...) . That's it. I don't want the popup/actionbar. I want the temporary thing that appears to the user, that says that the data was copied. On some devices, it's a toast, and on some (like G2) it's something else – android developer Feb 07 '16 at 12:00
  • @androiddeveloper, I think I know why I don't understand you! Because I just tried to copy some text from Hangouts in my Nexus5, and the only feedback I got was haptic (vibration), but nothing visual... I would suggest you to make your own custom feedback, whatever you decide it will be – Gavriel Feb 07 '16 at 12:10
  • @Gavriel I can add any feedback, but my question is how to use the default one that's used on EditText. About your case, it could be that your configuration was changed for this to occur, or that this is how it really works on your rom. If you open Hangouts in G2, and then you select text and choose to copy it, you will get the temporary note on the side to appear. On Note 4, when you go to WhatsApp and copy text, it shows "copied to clipboard" , and if it was already copied it says "item already copied to clipboard". – android developer Feb 07 '16 at 12:16
  • @androiddeveloper, all the examples you bring just make my point even clearer: it is not a standard behaviour! I've never heard that G2 or Note4 is the "standard" for anything, but I did hear that of my Nexus5. I see the bubble on WhatsApp too, but not in any of the google apps (Chrome, Hangouts). I think it's a custom feature used by some apps. It also explains why it is different in different apps / bloatwares(samsung vs nexus) – Gavriel Feb 07 '16 at 12:52
  • @Gavriel I didn't write "standard". I wrote "built in". I want to invoke the one that's used globally. And it is used in all apps that have EditText and didn't change how they behave. As an example, run Gmail on G2, compose a letter, write something and copy selected text inside the letter. You will see G2's UI for the copying. The code I've written above does it silently. – android developer Feb 07 '16 at 13:21
  • Can you test it on a nexus and tell me where do you see it in any of the official google-made apps? – Gavriel Feb 07 '16 at 13:27
  • @Gavriel On Nexus 5 with Android 6.0.1 it doesn't show anything. Not in Chrome, and not in Hangouts. – android developer Feb 07 '16 at 13:57
  • And not in 5.0.1 either. I rest my case. Do we agree now that it's not some build-in or standard or anything? It's somehow made by some of the components, either apps or some part of the android that was modified by some of the ROM vendors – Gavriel Feb 07 '16 at 14:02
  • @Gavriel Again, I didn't write that it's a standard. I want to invoke what's built in. – android developer Feb 07 '16 at 15:31

1 Answers1

0

As far as I know there's no such a built-in feature. You can make similar behaviour within your app. See all the examples in the comments below the question.

Gavriel
  • 18,880
  • 12
  • 68
  • 105