Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling copy and paste in webview android

Tags:

android

Is it possible to select the text from a webview then to copy and paste. Is there any special method to do this??Please help me..

like image 636
Faison N.P Avatar asked May 14 '12 12:05

Faison N.P


2 Answers

Hope this will help you...

public void selectAndCopyText() {
     try {
         Method m = WebView.class.getMethod("emulateShiftHeld", Boolean.TYPE); 
            m.invoke(BookView.mWebView, false); 
        } catch (Exception e) {
            e.printStackTrace();
            // fallback
            KeyEvent shiftPressEvent = new KeyEvent(0,0,
                 KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
            shiftPressEvent.dispatch(this);
        }

}

override the touch events

private void emulateShiftHeld(WebView view)
    {
        try
        {
            KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                    KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
            shiftPressEvent.dispatch(view);
            Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Log.e("dd", "Exception in emulateShiftHeld()", e);
        }
    }
like image 148
user755278 Avatar answered Oct 21 '22 17:10

user755278


According to this site of someone's blog says " Copy functionality in a WebView is available by default in Android 3.0 and above", and may be this information may be help, Android: how to select texts from webview

like image 25
hafidh Avatar answered Oct 21 '22 17:10

hafidh