Processing to Android visualization on Android

edited March 2018 in Android Mode

I am running Processing Sketches on Android Studio. Did it through exporting the project from Processing to Android, checking all the libraries. Then started a new Android Project, added these libraries, and that's it.. Now the question is how to project them in a visually appealing way on the activity layout.. For instance, how can I control how big the area is a Processing Sketch is displayed on. Another question is how to load a number of Processing Sketches in different places on the Layout.

Does anyone have experience on this, can share some code, point me to some sketches...?

THANKS!!!!

public class MainActivity extends AppCompatActivity {  

private PApplet sketch;
private PApplet secondsketch;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));


    sketch = new weatherBrussels();

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


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


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

}

Answers

  • edited March 2018

    I have tried the following to load two Processing sketches underneath each other..

    Layout file: two FrameLayouts, each for one Processing Sketch..

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.gebruiker.myapplication.MainActivity">
    
        <FrameLayout
            android:id="@+id/FrameLayoutWeather"
            android:layout_width="327dp"
            android:layout_height="226dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.512"
            app:layout_constraintStart_toStartOf="parent">
    
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/FrameLayoutSketch"
            android:layout_width="327dp"
            android:layout_height="214dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
        </FrameLayout>
    
    </android.support.constraint.ConstraintLayout>
    

    And the MainActivity..

    public class MainActivity extends AppCompatActivity {
    
        private PApplet sketch;
        private PApplet secondsketch;
        FrameLayout frameLayout_weather;
        FrameLayout frameLayout_sketch;
    
        @ Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            frameLayout_sketch = findViewById(R.id.FrameLayoutSketch);
            frameLayout_weather = findViewById(R.id.FrameLayoutWeather);
    
            sketch = new weatherBrussels();
            PFragment fragment_sketch = new PFragment(sketch);
            fragment_sketch.setView(frameLayout_sketch, this);
    
            secondsketch = new second_sketch();
            PFragment fragment_weather = new PFragment(secondsketch);
            fragment_weather.setView(frameLayout_weather, this);
    
        }
    
        @ Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[],
                                               int[] grantResults) {
            if (sketch != null) {
                sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            if (secondsketch != null) {
                secondsketch.onRequestPermissionsResult(requestCode, permissions,grantResults);
            }
        }
    
        @ Override
        public void onNewIntent(Intent intent) {
            if (sketch != null) {
                sketch.onNewIntent(intent);
            }
            if (secondsketch!= null){
                secondsketch.onNewIntent(intent);
            }
        }
    }
    

    I do not get an error message, but the Android Studio Project does not work either.

    Any ideas?

  • @nvwingh===

    • a lot of problems there...first one is that you never add any fragment as it must be done, with a FragmentManager instance: so nothing can be seen!
  • So I can just treat PFragments as Fragments then, ok.. Since I want my sketches to be loaded when the page is loaded, just in different views, can I also use other View types, like CardView or ListView? Or with Processing sketches, am I obliged to use the PFragment (Fragment) approach?

    Thanks for your help!

  • Answer ✓

    @nvwingh===

    with P5 you are in a fragment; but you can add all kind of android views to this fragment: i have put in the forum many examples for that: textView, videoView; not tried with listView but it s probably the same. As for adding (not replacing) another fragments i dont think that it is possible, except perhaps using nested fragments: the first one is created by the main activity and it contains your first sketch; when it is created it creates a second fragment with another sketch using a fragment transaction with getChildFragmentManager(): i have not tested that but logically it could work...I ' ll have a look.

  • sounds great, where can I find these examples? I have checked your profile, but since you have over 1,5K posts it is a bit hard to find them.. :-)

  • I have found an answer to my questions.. Answer lie in setting a Static ID to my FrameLayouts and then losing the "SetContentView()" method..

    package com.example.gebruiker.myapplication;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.FrameLayout;
    import processing.android.PFragment;
    import processing.core.PApplet;
    
    public class MainActivity extends AppCompatActivity {
    
        private PApplet sketch1;
        private PApplet sketch2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FrameLayout frame1 = new FrameLayout(this);
            frame1.setId(R.id.FrameLayoutTwo);
    
            FrameLayout frame2 = new FrameLayout(this);
            frame2.setId(R.id.FrameLayoutOne);
    
            sketch1 = new sketchone();
            PFragment fragment1 = new PFragment(sketch1);
            fragment1.setView(frame1, this);
    
            sketch2 = new second_sketch();
            PFragment fragment2 = new PFragment(sketch2);
            fragment2.setView(frame2, this);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            if (sketch1 != null) {
                sketch1.onRequestPermissionsResult(
                        requestCode, permissions, grantResults);
            }
    
            if (sketch2 != null) {
                sketch2.onRequestPermissionsResult(
                        requestCode, permissions, grantResults);
            }
    
        }
    
        @Override
        public void onNewIntent(Intent intent) {
            if (sketch1 != null) {
                sketch1.onNewIntent(intent);
            }
    
            if (sketch2 != null) {
                sketch2.onNewIntent(intent);
            }
        }
    
Sign In or Register to comment.