android - Cordova WebView in a Dialog -
can implement cordova webview in customdialog in android? want click button , show me dialog webview. tried in way didn't work.
button = (button) findviewbyid(r.id.buttonshowcustomdialog); // add button click listener button.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // custom dialog final dialog dialog = new dialog(context); dialog.setcontentview(r.layout.custom); dialog.settitle("title..."); cwv = (cordovawebview) findviewbyid(r.id.webview); config.init(this); cwv.loadurl("file:///android_asset/www/index.html"); dialog.show(); } });
update2:
in fact, extended dialog doesn't event need implement cordovainterface. needs override setcontentview, , that's enough.
public class cordovadialog extends dialog { private context currentcontext; public cordovadialog(context context) { super(context); this.currentcontext = context; } // have override because need disable attaching root when inflating (wtf cordova ??) @override public void setcontentview(int layoutresid) { final layoutinflater inflater = layoutinflater.from(this.currentcontext); view v = inflater.inflate(layoutresid, null, false); super.setcontentview(v); }; }
i have same issue. have tried create class extends dialog , implements cordovainterface, didn't have luck witjh either. seems every time call setcontentview, cordova can't find activity associated dialog, , logcat shows warning saying activity doesn't implement cordovainterface does.
update: ok, figured out. here's how dit it. it's long works.
- first of all, let's assume parent activity, 1 creating dialog, implementing cordovainterface. also, let's cordovawebview inside layout.
- make new class (cordovadialog example) extends dialog , implements cordovainterface.
- make new constructor cordovadialog class passes context , interface can set cordovainterface parent activity (which should implement cordovainterface).
- override setcontentview in cordovadialog inflates view without attaching root (last params set false).
in main activity, create dialog, call config.init(), , call loadurl cordovawebview.
public class cordovadialog extends dialog implements cordovainterface { cordovainterface parentcordovainterface; context currentcontext;
public cordovadialog(context context, cordovainterface ci) { super(context); this.parentcordovainterface = ci; this.currentcontext = context; } @override public void setcontentview(int layoutresid) { final layoutinflater inflater = layoutinflater.from(this.currentcontext); view v = inflater.inflate(layoutresid, null, false); super.setcontentview(v); }; @override public activity getactivity() { return this.parentcordovainterface.getactivity(); } @override public executorservice getthreadpool() { return this.parentcordovainterface.getthreadpool(); } @override public object onmessage(string arg0, object arg1) { return this.parentcordovainterface.onmessage(arg0, arg1); } @override public void setactivityresultcallback(cordovaplugin plugin) { this.parentcordovainterface.setactivityresultcallback(plugin); } @override public void startactivityforresult(cordovaplugin command, intent intent, int requestcode) { this.parentcordovainterface.startactivityforresult(command, intent, requestcode); } }
and in activity implements cordovainterface:
final cordovadialog dialog = new cordovadialog(this, this); dialog.setowneractivity(this); dialog.setcontentview(r.layout.dialog_with_cordovawebview); cordovawebview cwv = (cordovawebview) dialog.findviewbyid(r.id.webviewdialog); config.init(); cwv.loadurl("file:///android_asset/www/index.html"); dialog.show();
Comments
Post a Comment