9

When you long press on something in Android, a context menu comes up. I want to add something to this context menu for all TextViews in the system.

For example, the system does this with Copy and Paste. I would want to add my own, and have it appear in every application.

Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
  • 1
    For anyone browsing this question now the answer is here: http://stackoverflow.com/questions/12995439/custom-cut-copy-action-bar-for-edittext-that-shows-text-selection-handles – Richard Friedman Jun 01 '13 at 19:31

5 Answers5

8

Add intent-filter in your file android-manifest.xml:

<activity
   android:name=".ProcessTextActivity"
   android:label="@string/process_text_action_name">
  <intent-filter>
     <action android:name="android.intent.action.PROCESS_TEXT" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="text/plain" />
 </intent-filter>
</activity>

Get highlighted by user text in activity your app in method onCreate():

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.process_text_main);
    CharSequence text = getIntent()
      .getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
    // process the text
}

More information in the article on medium.

Thanks for user: YungBlade

His answer on ru.stackoverflow.com

sam
  • 4,357
  • 5
  • 29
  • 34
Valeriy
  • 191
  • 2
  • 10
5

Currently Android does not support this, you cannot override or hook functionality globally at the system level without the particular activity implementing an intent or activity that you expose. Even in the case of publishing an intent it wouldn't matter unless the application running is a consumer... and all the base system applications and obviously all applications prior to yours would not be without updating the app to consume.

Basically as it stands, this is not possible.

What exactly are you trying to accomplish with this global context menu, some sort of global "Search For" or "Send To" functionality that runs through your application?

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • Thanks. I was trying to make a translate app that could translate any TextView - you would longpress, select translate, select the language, and it would appear in the textview. I could see it being useful in other places as well. Ah, well. Thanks, Isaac Waller http://www.isaacwaller.com/ – Isaac Waller Feb 20 '09 at 22:54
  • Oh yeah that would be nice.. you should publish an intent for that so people could subscribe, if it were available i'd probably use it my apps just FYI. Good Luck! – Quintin Robinson Feb 20 '09 at 23:03
  • It is possible, it's just not pretty :-\ – haseman Feb 20 '09 at 23:31
0

Push the Share button (tree of three dots) within selection menu. Then you should select your own app. Does't work for input field content, unfortunately.

0

Hmm; I don't know if you can extend built in types like in example. Ruby (my Java knowledge is not so deep enough).

However you can derive your own MyTextView from TextView. Then substitute all your TextView in layouts like this:

<TextView android:id="@+id/TextView01" />

to

<com.mydomain.mypackage.MyTextView android:id="@+id/TextView01" />

to automatically change type of all these fields.

Then you need to override all constructors (especially TextView(Context context, AttributeSet attrs) ).

After that all layout inflaters (automatic or manual) will create this type with no fuss.

Then you can create/register context menu callbacks as you wish.

Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
Marcin Gil
  • 68,043
  • 8
  • 59
  • 60
  • 2
    That only works for the TextViews in his apps/layouts. He wants to add something to all apps, including those for which he's got no authorship. Unless TextView uses some internal Intent to build its list of actions, it doesn't seem possible to get in there. – Ry4an Brase Feb 18 '09 at 09:30
  • Wouldn't that allow you to eg. watch someone's password in other apps? If you can attach to all TextViews with context menu it would be also possible to dump its contents... – Marcin Gil Feb 18 '09 at 10:34
  • 1
    It is possible in Android to watch somebody's password - with the proper permissions. – Isaac Waller Feb 19 '09 at 02:18
0

This might be a little bit hacky, but you can trap the menu key at the application/activity level, check to see if your current active view is a text entry view, and then build and display your own custom popup menu.

It is possible, it's just a little tricky.

If you create/inflate a TextView, call setFocusable(false) on it, and then set it as the active view your Activity will still receive key events. You will have to forward all those events (trackball, touch, key) to your View tree by hand. (Inside your "onKeyDown" function you'd have to call the appropriate "onKeyDown" method for the top level View) You effectively have to trap the notion of 'focus' and dole it out to the correct view yourself.

While a little ugly, it may give you the desired results.

This would, however, only work in your own application. Previous answers are correct about it being impossible across the entire phone.

haseman
  • 11,213
  • 8
  • 41
  • 38
  • How does this work on applications that you haven't built? If you open say the browser and select the navigation textview, well not to mention at this point your activity is gone.. how would you attach and receive the events? – Quintin Robinson Feb 20 '09 at 23:38