Processing for Android

Developing with Android Studio

Use Android Studio for advanced Android development with Processing's core library.

General steps

You should only use Android Studio for Android development if you've already spent time learning how to develop Android applications using Android Studio (separately from Processing), and want to make use of the Processing core library. Android development in Android Studio is significantly more difficult than desktop Processing/Java development with Eclipse. Take time to learn it if you're going this route: get a book, read some Android development tutorials, check Google's developer materials, etc.

In case you already know how to develop Android apps using this advanced IDE, it is not that difficult either. All our core code is bundled inside the android-core.zip file, which is placed inside the AndroidMode folder. You just need to rename this file to android-core.jar and add it as a dependency to your project. Step by step procedure for Android Studio is as follows:

1. Create an Android project if you haven't already created one:

New project

2. Select the target device:

Select target

3. Add an empty activity:

Add activity

4. Set the name for the main activity class and the layout file:

Customize activity

5. Copy android-core.jar to /app/libs, and add it as file dependency to the project:

Module settings

6. Then, write your sketch code by extending PApplet, for example:


// Sketch.java
package tutorials.androidstudio.fragmentsv3;

import processing.core.PApplet;

public class Sketch extends PApplet {
  public void settings() {
    size(600, 600);
  }

  public void setup() { }

  public void draw() {
    if (mousePressed) {
      ellipse(mouseX, mouseY, 50, 50);
    }
  }
}

7. Initialize the sketch in the main activity:


// MainActivity.java    
package tutorials.androidstudio.fragmentsv3;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = new Sketch();
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}    

8. Finally, create a simple layout for the main activity:

<!-- activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tutorials.androidstudio.fragmentsv3.MainActivity" >

    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>    

The complete Android Studio project resulting from these steps is available here.

Differences with version 4 of the mode

For version 4.0+, all the previous steps are still valid, the only difference is that we need to create a separate PFragment object in the main activity to contain the sketch:

import processing.core.PApplet;
import processing.android.PFragment;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getFragmentManager();
        PApplet sketch = new Sketch();
        PFragment fragment = new PFragment();
        fragment.setSketch(sketch);
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}

The complete Android Studio project for 4.0 is available here.

Multiple Processing sketches in a single app

Fragments give you the possibility of embedding several Processing sketches in a single Android app, check out this sample Android Studio project from Umair Khan to learn how (v3 only).