I am using webview for displaying content in Android Honeycomb(3.x). I created customized action menu for cut,copy and paste.How can i copy the selected text in Webview by using my customized action menu.
            Asked
            
        
        
            Active
            
        
            Viewed 5,082 times
        
    26
            
            
        - 
                    this could be of help... http://stackoverflow.com/questions/1111844/selecting-text-in-a-webview – Ron May 19 '12 at 07:06
- 
                    2Did the answer help you? Then please accept it. If not, please explain what does not work yet. – Dirk Jäckel Jun 20 '12 at 04:23
2 Answers
8
            
            
        May It Will Help...
public void selectAndCopyText() {
try {
    Method m = WebView.class.getMethod("emulateShiftHeld", null);
    m.invoke(this, null);
} catch (Exception e) {
    e.printStackTrace();
    // fallback
    KeyEvent shiftPressEvent = new KeyEvent(0,0,
         KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
    shiftPressEvent.dispatch(this);
}
}
- 
                    It works on Android 3.2! Some others answers suggested boolean parameter in emulateShiftHeld --- it is not correct! Use "null" as in code above and it will work for you! – Mike Keskinov Sep 21 '12 at 15:07
- 
                    Under Anroid 4.0.3 m.invoke(webView, null) cause native crash `09-21 23:20:49.151: A/libc(4103): Fatal signal 11 (SIGSEGV) at 0x00000014 (code=1)`. – Mike Keskinov Sep 21 '12 at 15:20
-2
            
            
        Try below code...
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);
    }
}
and Call above method wherever you want...
emulateShiftHeld(mWebView);
for more details see this... Android: how to select texts from webview
 
    
    
        Community
        
- 1
- 1
 
    
    
        Priyank Patel
        
- 12,244
- 8
- 65
- 85
- 
                    This is code for Android 2.2. Surej published code which works for both Android 2.2 & 3.2, but this is still problem for Android 4.* (Ice Cream Sandwich). The code above just do nothing under Ice Cream Sandwich. – Mike Keskinov Sep 27 '12 at 16:34
 
     
    