Drawing default Android over top of a Processing fragment


#1

Hi again,

I have another dumb question (again, I’m not an advanced Android dev).

I have a full screen Processing sketch, and I’m trying to place Android UI on top of it.

Pasting my Activity and Layout code below.

Thanks in advance for any pointers, and tips.

Cheers,
d.

MainActivity.java

package com.example.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import processing.android.PFragment;
import processing.core.PApplet;

public class MainActivity extends AppCompatActivity {

    private PApplet sketch;

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

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

    public void sendMessage(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    @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);
        }
    }
}

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="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.app.MainActivity" >

        <fragment
            android:id="@+id/test"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:background="@drawable/rounded_button"
            android:onClick="sendMessage"
            android:text="PLUGGED IN, READY TO GO"
            android:textColor="#FFFFFF"
            tools:layout_margin="50dp" />

</RelativeLayout>

#2

@derekkinsman ===

  • You seem to work with AS, ok?
  • are you sure that this code works (i am not asking about your button)?
  • You are trying to add a static fragment to your MainActivity (inside the relativeLayout) which is possible only if you have a class extending Fragment or AppCompatActivity (v7)
  • But your fragment is null (and has no name)
  • And you cannot reference the Sketch class which does not extend Fragment (but PApplet)
  • Instead you have to use the P5 way for creating the fragment (dynamically) and then add your button to the frameLayout using addView()

#3

@akenaton Oh yes, sorry, I’m working inside Android Studio at the moment.

This code currently works. I thought if I could statically place the fragment, I’d be able to also place the button next in the stack as to get it to draw over the fragment.

So, using the code example above, are you suggesting something like this for adding the button?

Again, thank you for all your help (here, and on the other questions you’ve helped with). Also, thanks for not just pasting in “working” code. Being pointed in the right direction definitely helps me learn the thing I’m trying to solve.

Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setText("@string/theText");
button.setId("@+id/button");
button.setOnClickListener(this);
frame.addView(frame);

// further down the page, same level as the onCreate
@Override
public void onClick(View v) {
  // Do the clicky button magic
}

#4

@derekkinsman===
yes: that is exactly what i mean (as for your btn.setOnClick Listener it could be much better adding immediately your code without the onClick(View v) ); when everything is ok i ll try to answer to the other question, supposing that you put some final snippet code with the P5 class “Sketch”, so i can “see” where you dont want touchEvents


#5

@akenaton omg mate, works perfectly. I owe you a [insert drink of your choice here]!

As for the other question, the whole of Sketch() should completely ignore touch events.

Basically the P5 stuff is working in the same way any other graphic is working. Imagine you have a page that exists as a ScrollView, and the page is 5 device screens tall. The first “screen” is a full screen Processing sketch. You then scroll down to read some text stuff. I’m looking to scroll a page without Processing trying to track the touch events.

Cheers again!