Webview for android in processing 3.x

GpmGpm
edited August 2016 in Android Mode

Hi, Pls can any one throw some light on using the webview for android in processing 3.x.

Basically, I want to load a webpage inside my p3 sketch. Pls help.

Thanks in advance.

Tagged:

Answers

  • `import android.app.Activity; import android.content.Context; import android.widget.FrameLayout; import android.view.ViewParent; import android.widget.RelativeLayout; import android.text.Editable; import android.graphics.Color; import android.widget.Toast; import android.os.Looper; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView;

    Activity act; FrameLayout fl;

    WebView webview;

    @Override
    public void onStart() { super.onStart();

    act = this.getActivity();

    webview = new WebView(act); webview.setLayoutParams(new RelativeLayout.LayoutParams( 800, 400 )); webview.setX(800); webview.setY(600); webview.getSettings().setJavaScriptEnabled(true);

    webview.loadUrl("https://processing.org/");

    fl = (FrameLayout)act.findViewById(0x1000); fl.addView(webview); }

    void settings() { fullScreen(); }

    void setup() {
    orientation(PORTRAIT); background(255, 0, 0); }

    void draw() { }`

    Hi @Kfrajer for the reply, however I'm getting an error on this line saying unexpected token : @Override

    Not able to understand what I have done wrong. Pls check. Thanks a lot.

  • @Gpm===

    this code works. You are overriding the onStart() method (fragment).

    format your code.

    put the exact error in the console.

  • GpmGpm
    edited September 2016

    HI @akenaton

    Here is the formatted code.

    import android.app.Activity;
    import android.content.Context;
    import android.widget.FrameLayout;
    import android.view.ViewParent;
    import android.widget.RelativeLayout;
    import android.text.Editable;
    import android.graphics.Color;
    import android.widget.Toast;
    import android.os.Looper;
    import android.view.WindowManager;
    import android.view.inputmethod.InputMethodManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.ViewGroup;
    
    import   android.webkit.WebView;
    
    Activity act;
    FrameLayout fl;
    
    WebView webview; 
    
    //@Override
    public void onStart() {
      super.onStart();
    
      act = this.getActivity();
    
      webview = new WebView(act);
      webview.setLayoutParams(new RelativeLayout.LayoutParams( 400, 600 ));
      webview.setX(400);
      webview.setY(600);
      webview.getSettings().setJavaScriptEnabled(true);
    
      webview.loadUrl("http:" + "//www.google.com/");
    
      fl = (FrameLayout)act.findViewById(0x1000);
      fl.addView(webview);
    }
    
    
    void settings() {
      fullScreen();
    }
    
    void setup() {   
      orientation(PORTRAIT);
      background(255, 250, 0);
    }
    
    void draw() {
    }
    

    I have taken out override. What I'm seeing is, the page getting opened in a browser after 2 seconds after app launches. Is there a way to control it so, it will be within app window. Thanks again.

  • GpmGpm
    edited September 2016

    once I paste the code, formatting is gone. how I can correct that. :-O

  • edited September 2016 Answer ✓

    @Gmp I tried akenaton suggestions and this is how to make it work:

    import android.app.Activity;
    import android.content.Context;
    import android.widget.FrameLayout;
    import android.view.ViewParent;
    import android.widget.RelativeLayout;
    import android.text.Editable;
    import android.graphics.Color;
    import android.widget.Toast;
    import android.os.Looper;
    import android.view.WindowManager;
    import android.view.inputmethod.InputMethodManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.ViewGroup;
    
    import   android.webkit.WebView;
    import   android.webkit.WebViewClient;
    
    Activity act;
    FrameLayout fl;
    
    WebView webview; 
    WebViewClient wbc;
    
    //@Override
    public void onStart() {
      super.onStart();
    
      act = this.getActivity();
    
      wbc = new WebViewClient();
    
      webview = new WebView(act);
      webview.setLayoutParams(new RelativeLayout.LayoutParams( 1200, 800 ));
      webview.setX(100);
      webview.setY(50);
      webview.setWebViewClient(wbc);
      webview.getSettings().setJavaScriptEnabled(true);
    
      webview.loadUrl("http://" + "www.google.com/");
    
      fl = (FrameLayout)act.findViewById(0x1000);
      fl.addView(webview);
    }
    
    
    void settings() {
      fullScreen();
    }
    
    void setup() {   
      orientation(PORTRAIT);
      background(255, 250, 0);
    }
    
    void draw() {
    }
    

    I hope this helps,

    Kf

  • @kfrajer : Thanks a lot. It is working!! :) @akenaton : Thanks for your suggestions. Thanks again.

Sign In or Register to comment.