i've app displaying college syllabus display bunch of html files stored assets in activity webview. app supports api 14+ html files plain text files. i'm trying provide share button copy text in webview , provide option share via shareintent copied text body. i'm able manually in webview long pressing on text , using select button , copying , pasting anywhere want. works perfectly. wanna replicate action click of button
here's code 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 doesn't seem working right, when press button previous contents of clipboard presented in toast , body of shareintent. wanna know how this, select whole text , copy string programmatically? or please tell me of other way can approach this
thank responses in advance
you can run java code (in android component) webview javascriptinterface.
you create javascript button return info function in activity/fragment/blabla.
the code shown below taken boris answer.
set new javascriptinterface webview:
javascriptinterface jsinterface = new javascriptinterface(this); webview.getsettings().setjavascriptenabled(true); webview.addjavascriptinterface(jsinterface, "jsinterface");
create class 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 on syllabus html:
<button onclick="window.jsinterface.sharestuff('your_selected_text');" > </button>
check these links more.
call java function javascript on android webview
how return value javascript in webview of android?
and examples in main documentation.
Comments
Post a Comment