2

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

Here's the code I tried :

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"));
}

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

Thank you for all the responses in advance

Image to show an example of the html file I'm displaying in my webview

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
user3908652
  • 131
  • 1
  • 2
  • 8
  • Why do you need to copy the text from the webview when you could just copy it from the HTML assest? – OneCricketeer Jan 24 '16 at 17:41
  • Just a quick off-response comment. I think that your layout design is very clean and good. If you want to find icons with alpha (transparent background) you can query Google's main source at: https://design.google.com/icons/ You can even set their color (tint) at runtime. – Evin1_ Jan 24 '16 at 19:35
  • Thank you, and I'll try out the links for the icons. – user3908652 Jan 26 '16 at 06:34

1 Answers1

0

You can run Java code (in your Android component) from a WebView with a JavaScriptInterface.

You would create a JavaScript button that would return the info to the function in your Activity/Fragment/Blabla.

The code shown below is taken from Boris answer.

Set a new JavaScriptInterface into your webView:

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

Create a class with the methods (optional creating new class).

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void shareStuff(String someStuff){
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, someStuff);
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
    }
}

And then on your syllabus HTML:

<button onclick="window.JSInterface.shareStuff('your_selected_text');" >
</button>

Check these links for more.

Call Java function from JavaScript over Android WebView

How to get return value from javascript in webview of android?

And some examples in the main documentation.

http://developer.android.com/guide/webapps/webview.html

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • I didn't understand the last bit. What does 'your_selected_text' mean. Do I have to put text there manually? I've over 500 html files for different courses and subjects offered by my university, so I can't do this for each of the files or manipulate all those files to add or remove something. I tried putting onclick="window.JSInterface.shareStuff('your_selected_text');" in my fab button and it didn't work on fab button click – user3908652 Jan 25 '16 at 14:12
  • The last part would go **inside** your HTML files, i.e., add a button with a JavaScript onClick function. Then it is fairly easy to retrieve the selected text with JavaScript, check this link for more: http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text – Evin1_ Jan 25 '16 at 14:28
  • Thank you so much for the answer. But I can not edit all the 500+ html files that I have, again. So I used a different approach to solve my problem. I'm converting the whole page to an image and then allowing it to be shared. But anyway thank you for your answer – user3908652 Jan 26 '16 at 06:31
  • You could add the button programmatically (only once). But your new approach is good too. – Evin1_ Jan 26 '16 at 06:37
  • I'm having trouble with my new approach as well.. The captured image is not complete, only the first portion of the syllabus is captured but the bottom part isn't. I don't know how to solve this – user3908652 Jan 26 '16 at 12:18