2

I want to know how I can override default text copying mechanism in Android to copy text along with meta data like URL if copying from browser, filename if copying from fine file to clipboard manager?

What would be the approach for implementing this universally on any Android device?

Updated

Use Case:

  • I open chrome on Android device.
  • Search something and open a website.
  • I select some text and copy it.
  • I open my app and paste the text.
  • Along with Text I want the website URL to be saved.

I can get copied text but not sure how to get the URL without separately copying it?

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60

2 Answers2

2

What would be the approach for implementing this universally on any Android device?

Android already supports the notion of a clipboard item having multiple pieces of data, as of Android 3.0. ClipData is the wrapper around the clipboard data, and it can have "one or Item instances, each of which can hold one or more representations of an item of data". Hence, there is nothing stopping developers from storing "URL if copying from browser, filename if copying from fine file" along with some text.

However:

  • You have no means of forcing Android app developers to create such clipboard items

  • You have no means of forcing Android app developers to consume such clipboard items (most apps will coerce the ClipData to a text representation and use that)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have no idea if Chrome puts the URL on the clipboard in your use case today, let alone whether they will do so in the future. The same hold true for any of the other browsers. You are certainly welcome to write an app to examine all the `Item` objects in the `ClipData` and see what they provide. If they provide the URL, use it. If not, you have no means of forcing the Chrome developers to put the URL on the clipboard. – CommonsWare Dec 22 '15 at 18:25
  • Nothing much... Text and html text are there... Uri is null :( – Abhinav Tyagi Dec 22 '15 at 18:45
  • Then, simply put, you are out of luck. The decision of what gets put on the clipboard is up to the user (by requesting a copy operation) and by the developers of the app the user is in (by deciding what "a copy operation" actually means). – CommonsWare Dec 22 '15 at 19:03
  • Can I intercept COPY event when user selects any text anywhere on device? – Abhinav Tyagi Dec 23 '15 at 15:37
  • @AbhinavTyagi I know of no way to do that. – CommonsWare Dec 24 '15 at 14:15
0

There is another way to copy text with url

You can use android WebView within your app and copy text from that WebView then you can use following code to get active url

String webUrl = webView.getUrl();

Hope it will solve your problem

Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41