views:

2778

answers:

3

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.

A: 

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

However you can derive your own MyTextView from TextView. Then substitute all your TextViews 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.

Marcin Gil
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
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
It is possible in Android to watch somebody's password - with the proper permissions.
Isaac Waller
A: 

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
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
+2  A: 

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
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
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
It is possible, it's just not pretty :-\
haseman