Search code examples
androidadd-onandroid-contextmenu

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?


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.


Solution

  • 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