-1

How to select text or highlight text in webview android 3.1 while clicking menu? I have implemented but it's not working. can anybody tell how to do it?

public boolean onOptionsItemSelected(MenuItem item){   
       switch(item.getItemId()){   
       case SELECTTEXT_MENU_ID:   
           //SelectText();
           System.out.println("entering menu");
           selectAndCopyText();
          return true;

       }   
       return true;   
    }  




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

Thanks for your help

kumar
  • 645
  • 2
  • 14
  • 24

1 Answers1

0

First of all, are you sure that's a convinient way of calling a webview's method?

Secondly, you should pay more attention to the docs, they say that method is deprecated, and not to rely on that functionality.

I think your best chance to access the html content and thus select/unselect any text is trough JavaScript. Create a JavaScript method to do that and call it from the webview.

mdelolmo
  • 6,417
  • 3
  • 40
  • 58