How to override Contextual Action Bar when selecting text in a WebView in Android? -
i've googled lot, , find tutorials , answers here in stackoverflow, facing difficults resolve issue.
i have fragment webview, , want show custom contextual action bar when user selects text of web view. have 2 main issues here:
- 1 currently, custom cab showed when user performs long click in part of web view.
- 2 text not selected when user performs long click in text of web view.
some of current code:
custom interface:
public class selectactionmodecallback implements actionmode.callback { @override public boolean oncreateactionmode(actionmode mode, menu menu) { return false; } @override public boolean onprepareactionmode(actionmode mode, menu menu) { return false; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { return false; } @override public void ondestroyactionmode(actionmode mode) { } }
custom webview
public class customwebview extends webview { private selectactionmodecallback actionmodecallback; public customwebview(context context, attributeset attrs) { super(context, attrs); } @override public actionmode startactionmode(callback callback) { actionmodecallback = new selectactionmodecallback(); return super.startactionmode(actionmodecallback); }
in fragment, have this:
@override public void onresume() { super.onresume(); mywebview.setonlongclicklistener(new view.onlongclicklistener() { public boolean onlongclick(view view) { if (mactionmode != null) { toast.maketext(getactivity(), "test", toast.length_short).show(); return false; } mactionmode = getactivity().startactionmode(mactionmodecallback); view.setselected(true); return true; } }); } private selectactionmodecallback mactionmodecallback = new selectactionmodecallback() { @override public boolean oncreateactionmode(actionmode mode, menu menu) { menuinflater inflater = mode.getmenuinflater(); inflater.inflate(r.menu.custom, menu); return true; } @override public boolean onprepareactionmode(actionmode mode, menu menu) { return false; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { switch (item.getitemid()) { case r.id.action_sustom: custommethod(); mode.finish(); return true; default: return false; } } @override public void ondestroyactionmode(actionmode mode) { mactionmode = null; } };
there better way! see following answer: https://stackoverflow.com/a/22391169/2608235
in short, can't use
onlongclicklistener
, keep selection within webview
. android weird stuff behind scenes selecting text within webviews
. if override onlongclicklistener
in order call startactionmode
, lose selection, have found out. what should instead override startactionmode
in fragment, rather parent view
(in case, customwebview
).
i don't have mod permission mark question duplicate, is.
see question more info: use custom contextual action bar webview text selection
Comments
Post a Comment