Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the "paste" event in Android 3.0(Honeycomb)?

I have a customized EditText, which need to do customized "paste".

I overrode onTextContextMenuItem(int id) to handle "paste" requested by selecting context menu.

@Override
public boolean onTextContextMenuItem(int id) {
  switch(id){
  case android.R.id.paste:
    doMyPaste();
    return true;
  }
}

This works in Android before 3.0.
In 3.0, however, there is a small "paste" widget near the cursor widget if it's long-pressed, or the cursor is tapped.
enter image description here
When user do "paste" from this widget, the onTextContextMenuItem(int id) won't be invoked. As a result, I can't do the customized paste.
Do any one knows what that small "paste" widget is? Which method should I overrode to do a my own "paste"?

like image 945
Ben Lee Avatar asked Jul 11 '11 09:07

Ben Lee


1 Answers

To cover all bases, this has to be API-specific, so you must commit to doing it two different ways anyway.

For new APIs, the new android.content.ClipboardManager interface provides everything you need to transfer any MIME type you want.

For old APIs, you must be tricksy if you expect to play with the old android.text.ClipboardManager. Just base-64 encode the data of your Image (or whatever) and send that as text. On the receiving side, just reverse the process.

You can even "auto-detect" by determining whether you have android.text.ClipboardManager or android.content.ClipboardManager and act accordingly!

Also, your handler method should be returning super.onTextContextMenuItem(id) if you don't process anything. Maybe an editing artifact?

As far as the Paste Widget, that's not present in old APIs, or may be present on certain OEM UIs, and you are probably left with implementing that yourself, or using a degraded method of interaction. Once you put text on the clipboard, the Paste command shows up in the "normal" context menus.

like image 177
escape-llc Avatar answered Oct 18 '22 03:10

escape-llc