6

How can we add a new context menu option to all the apps that would show up when user selects a text and long presses on it?

Let's say I want to provide user the capability of finding the meaning of a word from an offline dictionary without having to navigate away from his app context.

  1. In general to add a context menu we would do a registerForContextMenu on a particular TextView object. Given that this needs to work for all the apps, how do we add context menu options?
  2. How to read the selected text?
  3. How to render a pop-up control on top of the currently open app?

I'm quite new to android development. So any detailed answers or pointers to samples will be of great help. I'm assuming this should be possible to do as the apps "define", "define offline dictionary" seem to do something close to this if not exactly same. In these apps, you can select text and click on copy to see the meaning of the text.

All the search that I have done have shown me examples on how to render a context menu for a text view or how to get selected list item in the context menu handler. But that is not helpful.

Thanks in advance.

cp1
  • 75
  • 1
  • 5

1 Answers1

3

I think this can help you, says how to create the Context menu and get the selected text. with that you just need to do a intent to a dictionary giving that selected text as a extra:

https://nitesh.morajkar.com/how-to-select-and-share-text-with-intents-in-android/

And if you want to use for example the google translator for definitions this can help:

Android API support dictionary applications

EDIT: after some search by both the solution for this question is here: https://medium.com/google-developers/custom-text-selection-actions-with-action-process-text-191f792d2999#.h6377bc8j

Community
  • 1
  • 1
Joao Jorge
  • 91
  • 6
  • In the link you have provided, we seem to be doing this: tv = (TextView) findViewById(R.id.textview); tv.setCustomSelectionActionModeCallback(...) Doesn't it mean that we are adding the context menu only to the TextView element in our app? How would this work for all the other apps? Let's say I want to be able to show a new context menu option in adobe pdf reader. This would not work right? – cp1 Feb 22 '17 at 17:16
  • i think you cant..context menu is for a unique app you cant change code in other apps.. for that the other app needed to do a intent for a specific action like in this case when you select text and your app be capable of handle that action that is to search for a new defenition.. so the awnser i think is that you cant do that to other apps.. – Joao Jorge Feb 22 '17 at 17:23
  • What you are saying seems right. It makes sense not being able to extend features in other apps unless they explicitly let us do so. – cp1 Feb 22 '17 at 17:41
  • But, as I stated in the question earlier, there is this app called 'define' whose functionality is to show the meaning of a text that user copies in the notification bar. There is a close variant of this called 'define - offline dictionary' which shows the meaning of a copied text in the form of a pop-up with in current app(ex: adobe pdf reader). How is it able to do something of this sort? Does the copy to clipboard option in android provide a hook for us to be able to do something? How is 'define - offline dictionary' capable of rendering a pop-up in the context of a different app? – cp1 Feb 22 '17 at 17:42
  • ohh i see maibe is something like this you want, take a look.. https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Actions – Joao Jorge Feb 22 '17 at 17:56
  • Hmm.. not really. The link talks about usual notifications. For example something like a whatsapp notification. Whatsapp shows a notification msg when you receive a msg. When you click on the notification, an activity in the app is invoked which would handle popping up the right conversation for you. Here the notification is raised by whatsapp and on click of it is also handled by the same app. I'm looking for something that would work across apps. – cp1 Feb 22 '17 at 18:03
  • after finaly understand what you want, and with a little search you need a app with permission to draw over other apps.. Ex of this feature and a explanation: https://medium.com/@rotxed/drawing-over-other-apps-marshmallow-edition-987eff9f99a9#.f1g5r0qmv http://www.androidpolice.com/2015/09/07/android-m-begins-locking-down-floating-apps-requires-users-to-grant-special-permission-to-draw-on-other-apps/ And you need to do a intent to ask for this permission you found that here: http://stackoverflow.com/questions/32061934/permission-from-manifest-doesnt-work-in-android-6/32065680#32065680 – Joao Jorge Feb 23 '17 at 17:15
  • Now you just need to search for how to do it like I did for you to find what you needed – Joao Jorge Feb 23 '17 at 17:18
  • 2
    Found what I am looking for: [https://medium.com/google-developers/custom-text-selection-actions-with-action-process-text-191f792d2999#.ytta438f7](https://medium.com/google-developers/custom-text-selection-actions-with-action-process-text-191f792d2999#.ytta438f7). For all the efforts that you have taken, I would like to give you credit by accepting your post as an answer. Can you please edit it to contain this link. – cp1 Feb 24 '17 at 01:50