Android Studio + Processing

Hi, people :wave:!!

After a while trying it, finally I made it. I’m capable to launch Sketches from P3 (Processing 3) in Android Studio and pass them some values. I don’t have many technical knowledge, but no one told me how to do it, so I want to share this example to people who have the same goal, to save time.

Here is the code in AS:

MainActivity:

package processing.test.mixing1;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import processing.android.CompatUtils;
import processing.android.PFragment;
import processing.core.PApplet;
import static processing.core.PApplet.println;


public class MainActivity extends AppCompatActivity{
  private PApplet sketch;
  Button next,fragment1,fragment2;  // this are the buttons created in the activity_main.xml
     //next is for other activity (Main2Activity), fragment1 to start the sketch mixing1, 
    // and fragment2 to start other sketch (mixing2)
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    next = (Button)findViewById(R.id.btn1); //this are the id 
    fragment1 = (Button)findViewById(R.id.btn2) ; //taken from the xml 
    fragment2 = (Button)findViewById(R.id.btn3) ; //when you crate the buttons
   
 fragment1.setOnClickListener(new View.OnClickListener() { 
      @Override //the method to start the sketch mixing1 with the button fragment1
      public void onClick(View v) {
        Context a = getApplicationContext();
        FrameLayout frame = new FrameLayout(a);
        frame.setId(CompatUtils.getUniqueViewId());
        setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                    ViewGroup.LayoutParams.MATCH_PARENT));

        sketch = new mixing1(); // mixing1 is the name of the sketch from P3

      PFragment fragment = new PFragment(sketch);
      fragment.setView(frame, MainActivity.this);
      }
    });

     //The same for the other sketch
    fragment2.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        Context a = getApplicationContext();
        FrameLayout frame = new FrameLayout(a);
        frame.setId(CompatUtils.getUniqueViewId());
        setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        sketch = new mixing2();

        PFragment fragment = new PFragment(sketch);
        fragment.setView(frame, MainActivity.this);
      }

      }); //////////////////////////////
     // Similar for the other Activity
    next.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent next = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(next);
      }
    });

  }

  // this function was just to try pass values that changes over the time
  public static int getInt(){
    int b =(int)(Math.random()*900);
    return b;
  }

//// All this down functions are created when you export the sketch to AS

  @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (sketch != null) {
      sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  }

  @Override
  public void onNewIntent(Intent intent) {
    if (sketch != null) {
      sketch.onNewIntent(intent);
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (sketch != null) {
      sketch.onActivityResult(requestCode, resultCode, data);
    }
  }

  @Override
  public void onBackPressed() {
    if (sketch != null) {
      sketch.onBackPressed();
    }
  }
}

The sketch 1 exported from P3 to AS with the export option:
(is just an example to see different sketches)

package processing.test.mixing1;

import android.content.Intent;
import processing.core.PApplet;

public class mixing1 extends PApplet {

  public void setup() {
  }

  public void draw() {
    background(0);
    ellipse(width / 2, height / 2, MainActivity.getInt(),MainActivity.getInt() );
 // receiving the values from the MainActivity 
  }
  public void mousePressed(){ // Did this to go back to the MainActivity
    Intent gBack = new Intent(getActivity(),MainActivity.class);
    startActivity(gBack);
  }
  @Override
  public void onBackPressed() {// Did this to go back to the MainActivity
    Intent gBack = new Intent(getActivity(),MainActivity.class);
    startActivity(gBack);
  }
  public void settings() {
    size(displayWidth, displayHeight);
  }
}

For the other Sketch, I just created a new java Class, deleted all in that class, and paste my P3 code:
(this sketch was just to see if when I create a not full Height/Width sketch It fills the whole window or I can see the previous view at the back, but spoiler… no, you can’t see the previous view)

package processing.test.mixing1;

import android.content.Intent;
import processing.core.PApplet;

public class mixing2 extends PApplet {

    public void setup() {
    }

    public void draw() {
        background(255);
        fill(0);
        ellipse(width / 2, height / 2, 100, 100);
    }

    public void mousePressed(){ // the same as the first sketch
        Intent gBack = new Intent(getActivity(),MainActivity.class);
        startActivity(gBack);
    }
    @Override
    public void onBackPressed() { // the same as the first sketch
        Intent gBack = new Intent(getActivity(),MainActivity.class);
        startActivity(gBack);
    }

    public void settings() {
        size(displayWidth/2, displayHeight/2);
    }
}

On the second Activity I just placed a text in the xml… nothing special, the code in java is the default created when you crate the new class, going to paste it, but is not relevant:

package processing.test.mixing1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

Sorry for the extension, feel free to modify it without losing important information.

I made it starting with an AS exported from P3, and touching the code.

Thanks, and hope it helps to someone.
Expecial thanks to @akenaton who warned me about sketches are fragments

:hugs:

3 Likes