2

I need to get the selected text from webview and have to highlight the text permanently. I tried as selecting the text and while i am attempting to get the text using clipboard it showing me null pointer exception at "clipboard.getText()". I have tried code as

ClipboardManager mClipboard =
        (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

 KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                    KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(webview);
 if(mClipboard!=null)
     {
       String text = mClipboard.getText().toString();
       Toast.makeText(this, "select_text_now     "+text, Toast.LENGTH_LONG).show();
     }

I need to get the selected text for highlight futher..please help me..

deepa
  • 2,496
  • 1
  • 26
  • 43
  • an error log would be much appreciated – Triode Feb 28 '12 at 11:49
  • Its null pointer exception at String text = mClipboard.getText().toString(); – deepa Feb 28 '12 at 11:59
  • mClipboard.getText() if this doen't contain any values then it will through nu point exception, may be you can put an if condition to check the getText() methord id returning a string or not, correct me if i am wrong – Triode Feb 28 '12 at 12:19
  • null pointer exception occurs at Android4.0 but in android2.2 its showing correctly.i dont know why this happening.. – deepa Feb 28 '12 at 12:24
  • String text = (mClipboard.getText() != null) ? mClipboard.getText().toString() : ""; this is not a solution for ur problem but null point exception can be avoided, thanks – Triode Feb 28 '12 at 12:27
  • Do you found any way to get selected word from javascript that window.getSelection() returning null in android – Ravi Nov 09 '12 at 06:32
  • @Dainel no i didnt find the solution for this. – deepa Nov 09 '12 at 06:36
  • me also facing the same problem tried a lot of things but all are useless still returning null – Ravi Nov 09 '12 at 06:43

1 Answers1

2

I have filed an Android bug for this: Issue 24842: WebView should make getSelection public. If you look at the ticket, you'll see some various workarounds.

Note that the code you posted is roughly similar to emulateShiftHeld which is deprecated in 4.0, which is probably why it is working in 2.2 and not in 4.0.

j-dexx
  • 10,286
  • 3
  • 23
  • 36
louielouie
  • 14,881
  • 3
  • 26
  • 31
  • Any another alternative to get the selected word with tag name and id in android webview – Ravi Nov 09 '12 at 06:33