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.

1

5 Answers 5

9

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

5
+100

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?

3
  • 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 isaacwaller.com Feb 20, 2009 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! Feb 20, 2009 at 23:03
  • It is possible, it's just not pretty :-\
    – haseman
    Feb 20, 2009 at 23:31
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.

1
  • 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? Feb 20, 2009 at 23:38
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.

3
  • 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. Feb 18, 2009 at 9: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, 2009 at 10:34
  • 1
    It is possible in Android to watch somebody's password - with the proper permissions. Feb 19, 2009 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.