title: How to select text from android webview programmatically
body: <p>I've an app for displaying my college syllabus where I display a bunch of html files stored as assets in an activity with a webview. The App supports only API 14+
  The html files are plain text files.
  What I'm trying to do now is provide a share button which would copy the all the text in the webview and provide option to share it via the shareintent with the copied text as the body.
  I'm able to do this manually in the webview by long pressing on text and using select all button and copying it and then pasting it anywhere I want. This works perfectly. 
  I just wanna replicate this action with the click of a button</p>
  
  <p>Here's the code I tried :</p>
  
  <pre><code>android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
  KeyEvent shiftPressEvent = new KeyEvent(0, 0,
          KeyEvent.ACTION_DOWN,
          KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
  shiftPressEvent.dispatch(webView);
  
  if(clipboard!=null) {
      String text = clipboard.getText().toString();
      Toast.makeText(SyllabusPage_alternative.this, "select_text_now     " + text, Toast.LENGTH_LONG).show();
  
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
      sharingIntent.setType("text/plain");
      String shareBody = text;
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
      startActivity(Intent.createChooser(sharingIntent, "Share via"));
  }
  </code></pre>
  
  <p>But this doesn't seem to be working right, when I press the button only the previous contents of the clipboard are presented in the toast and the body of the shareIntent. 
  I just wanna know how to do this, select the whole text and then copy it into a string programmatically? Or Please Tell me of any other way I can approach this</p>
  
  <p>Thank you for all the responses in advance</p>
  
  <p><a href="https://i.stack.imgur.com/qbI90.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qbI90.png" alt="Image to show an example of the html file I&#39;m displaying in my webview"></a></p>