Friday, September 3, 2010

[android-developers] Re: User selectable text in WebView

FWIW, I was able to obtain the clipboard text by extending the WebView
class, adding my own invalidate method, and a callback. The end result
was something like this:

public MyWebView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
mClipboard = (ClipboardManager)
context.getSystemService(Context.CLIPBOARD_SERVICE);
}

public void invalidate() {
super.invalidate();

if (mClipboard != null && mClipboard.hasText() && mOnClipboardText !
= null) {
mOnClipboardText.run();
mClipboard.setText(null);
}
}

public void setOnClipboardText(Runnable r) {
this.mOnClipboardText = r;
}

In the Activity that contains the webview, you will change the
existing WebView object to the new WebView, grab an instance to the
clipboard manager, and set the callback to do what you need to do.
Example:

onCreate(...) {
...
mWebView.setOnClipboardText(new Runnable() {
public void run() {
if (mClipboard != null) {
String text = mClipboard.getText().toString();
Log.i(TAG, "Clipboard text: " + text);
Toast.makeText(ReaderActivity.this, text, Toast.LENGTH_SHORT).show();
}
}
});

-Dan

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment