How to get selected text in Android web browser

I need to get selected text from webview and constantly select text. I tried to select the text, and while I am trying to get the text using the clipboard, it shows me the null pointer exception in "clipboard.getText ()". I tried the 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 selected text to highlight futher..please help me ..

+2
source share
1 answer

I logged an Android error for this: Problem 24842: WebView should make getSelection public . If you look at the ticket, you will see several workarounds.

Note that the code you posted is approximately similar to emulateShiftHeld, which is deprecated in 4.0, which is probably why it works in version 2.2, not version 4.0.

+2
source

Source: https://habr.com/ru/post/981934/


All Articles