android - Can't get selected text from webview -
i trying selected text webview below code returning null.please let me problem in below code.
in code index.html html file inside sdcard/dheeraj/index.html folder.
import android.app.activity; import android.content.res.assetmanager; import android.os.bundle; import android.os.environment; import android.content.clipboardmanager; import android.util.log; import android.view.actionmode; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.webkit.websettings; import android.webkit.webview; import android.widget.toast; public class addtonoteswithwebviewactivity extends activity { assetmanager assetmanager; webview webview; string line, line1 = "", finalstr = ""; int = 0; private actionmode mactionmode = null; private clipboardmanager clipboard; boolean mark_text=false; @override public void onactionmodestarted(actionmode mode) { if (mactionmode == null) { mactionmode = mode; menu menu = mode.getmenu(); // remove default menu items (select all, copy, paste, search) menu.clear(); mode.getmenuinflater().inflate(r.menu.add_to_notes_menu, menu); } super.onactionmodestarted(mode); } public void oncontextualmenuitemclicked(menuitem item) { switch (item.getitemid()) { case r.id.add_to_notes_menu_item: clipboard = (clipboardmanager) getsystemservice(clipboard_service); emulateshiftheld(webview); selectedtext(); break; default: break; } // true, check anyway, in case if (mactionmode != null) { mactionmode.finish(); } } private void selectedtext() { toast.maketext(this, clipboard.gettext(), toast.length_short).show(); } @override public void onactionmodefinished(actionmode mode) { mactionmode = null; super.onactionmodefinished(mode); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.ac_add_to_notes_web_view); webview=(webview)findviewbyid(r.id.web_view_browser); websettings websettings = webview.getsettings(); websettings.setjavascriptenabled(true); webview.loadurl("file://" + environment.getexternalstoragedirectory() + "/dheeraj/index.html"); } private void emulateshiftheld(webview view) { try { keyevent shiftpressevent = new keyevent(0, 0, keyevent.action_down, keyevent.keycode_shift_left, 0, 0); shiftpressevent.dispatch(view); registerforcontextmenu(view); } catch (exception e) { log.e("dd", "exception in emulateshiftheld()", e); } } }
please help....?????????
you can refer following code :
package com.demo.selecttext; import android.app.activity; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.webkit.webview; public class mainactivity extends activity { webview mwebview; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mwebview = (webview)findviewbyid(r.id.webview); mwebview.loadurl("http://devemat-androidprogramming.blogspot.com/"); } private static final int selecttext_menu_id = menu.first; @override public boolean oncreateoptionsmenu(menu menu){ super.oncreateoptionsmenu(menu); menu.add(0, selecttext_menu_id, 0, "select text"); return true; } public boolean onprepareoptionsmenu(menu menu){ super.onprepareoptionsmenu(menu); return true; } public boolean onoptionsitemselected(menuitem item){ switch(item.getitemid()){ case selecttext_menu_id: selecttext(); return true; } return true; } public void selecttext(){ try{ keyevent shiftpressevent = new keyevent(0, 0, keyevent.action_down, keyevent.keycode_shift_left, 0, 0); shiftpressevent.dispatch(mwebview); }catch(exception e){ throw new assertionerror(e); } } }