Question

Can I implement a Cordova WebView in a CustomDialog in android? I want to click a button and this show me a dialog with the webview. I tried in this way but 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();
    }
});
Was it helpful?

Solution

UPDATE2:

In fact, the extended Dialog doesn't event need to implement CordovaInterface. It just needs to override setContentView, and that's enough.

public class CordovaDialog extends Dialog {
    private Context currentContext;

    public CordovaDialog(Context context) {
        super(context);
        this.currentContext = context;
    }

    // we have to override this because we need to disable attaching to 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 the same issue. I have also tried to create a class which extends Dialog and implements CordovaInterface, but didn't have any luck witjh that either. It seems every time I call setContentView, Cordova just can't find the Activity associated to the Dialog, and logcat shows a warning saying that my Activity doesn't implement CordovaInterface but it does.

UPDATE: Ok, I figured it out. So here's how I dit it. It's long but it works.

  1. First of all, let's assume that the parent Activity, the one which is creating the dialog, is already implementing CordovaInterface. Also, let's say that your CordovaWebview is inside a layout.
  2. Make a new class (CordovaDialog for example) which extends Dialog and implements CordovaInterface.
  3. Make a new constructor for the CordovaDialog class which passes the context and the interface so you can set the CordovaInterface from parent activity (which should also implement CordovaInterface).
  4. Override setContentView in the CordovaDialog so that it inflates the view without attaching to root (last params set to false).
  5. In your main activity, create the dialog, call Config.init(), and call loadUrl for 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 then in your the activity which 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top