Creating Native Android Menus in Processing Android Mode

Has anyone tried to get Native Android menus from within Processing Android Mode? It would be great to make apps that have the full android look and feel.

I'm not sure which folder the menu.xml file would go but I'm thinking about something like the code below:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
//need to get menu.xml somehow

void setup()
{
orientation(LANDSCAPE);
}

public class MenuOptionsDemoProject extends Activity
{
/* Called when the activity is first created. /
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);//commented out - not working
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//MenuInflater inflater = getMenuInflater();//commented out - not working
//inflater.inflate(R.menu.my_menu, menu);//commented out - not working
return true;
}
}
void draw()
{

}

Answers

  • thanks for the example kfrajer.

    Unfortunately my android skills are not good enough to adapt this example to a menu widget, so I've decided to learn Android Studio development in order to understand this, rather than keep asking questions!

    So - off topic - Instead of bringing Android to Processing I am going to try and bring processing to Android :)

    So far I have imported a processing sketch to Android Studio as a fragment and compiled a working Native Android app. I'm trying to connect a button to a variable in the processing fragment, but I don't know how to get Android to communicate with the variables in the fragment.

  • @chrios1001===

    i think it is the good choice to migrate to AS or Eclipse for android... i dont understand your question about variable; can you post some code with this problem?

  • You will need to override a listener of your widget. If you post your code, it will help. Also what type of widgets do you see using?

    Kf

  • I'm using the standard processing android example code from here https://github.com/codeanticode/processing-android-tutorials/tree/master/android_studio/ex2_fragmentsv4

    I'm Trying to link the variable "ellipseSize" to a button or menu item in MainActivity.java How do we make the variable visible elsewhere in the project?

    Sketch.java

    package tutorials.androidstudio.fragmentsv4;
    
    import processing.core.PApplet;
    
    public class Sketch extends PApplet {
      int ellipseSize=200;
      public void settings() {
        size(600, 600);
      }
    
      public void setup() {
      }
    
      public void draw() {
        if (mousePressed) {
          ellipse(mouseX, mouseY, ellipseSize, ellipseSize);
        }
      }
    }
    
  • edited May 2017

    I think I have made some progress on this - So I found an example of native menus in processing android here: https://lernprocessing.wordpress.com/2011/03/11/processing-apps-fur-android-optionsmenu/

    Which, translated from german and with some tweaks compiles successfully, but there's no way to make the menu appear - this example is from 2011 - maybe android UI has changed too much? Does anyone know how to make the menu appear?

    // Imports for displaying the menu
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.view.Menu;
    import android.view.MenuInflater; 
    import android.view.MenuItem;
    import android.view.View;//may not need all of these
    
    
    int corners = 5; // Number of corners at startup
    int angle;
    float x1, y1, x2, y2;
    
     // IDs of the menu items are assigned
    public static final int DREI_ID =  Menu.FIRST;
    public static final int VIER_ID =Menu.FIRST+2;
    public static final  int FUENF_ID = Menu.FIRST+3;
    public static final int SECHS_ID = Menu.FIRST+4;
    public static final int ACHT_ID = Menu.FIRST+5;
    
    void setup() {
     smooth();
    }
    
    void draw() {
     fill(255, 20);
     rect(0, 0, width, height);
     polygon(corners, mouseX, mouseY, 10, 10);
    }
    
    void  polygon (int pages, int x, int y, int radiusX, int radiusY) {
     angle = (int) 360/pages;
     for (int grade=0; grade<360; grade+=angle) {
     x1 =sin(radians(grade))*radiusX+(x);
     y1 =cos(radians(grade))*radiusY+(y);
     x2 =sin(radians(grade+angle))*radiusX+(x);
     y2 =cos(radians(grade+angle))*radiusY+(y);
     line(x1, y1, x2, y2);
     }
    }
    
    public class MenuOptionsDemoProject extends Activity/* Called when the activity is first created. */
    {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      // here the individual menu entries are generated
     menu.add(0, DREI_ID, Menu.NONE, "3");
     menu.add(1, VIER_ID, Menu.NONE, "4");
     menu.add(2, FUENF_ID, Menu.NONE, "5");
     menu.add(3, SECHS_ID, Menu.NONE, "6");
     menu.add(4, ACHT_ID, Menu.NONE, "8");
     return super.onCreateOptionsMenu(menu);
    }
    
     // is called when a menu item has been selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      // the selection is checked via the ItemId of the menu item
     switch (item.getItemId()) {
     case DREI_ID:
     corners=3;
     break;
    
     case VIER_ID:
     corners=4;
     break;
    
     case FUENF_ID:
     corners=5;
     break;
    
     case SECHS_ID:
     corners=6;
     break;
    
     case ACHT_ID:
     corners=8;
     break;
     }
     return super.onOptionsItemSelected(item);
    }
    
    }
    
  • edited May 2017

    "<_a href="/two/profile/Override">@O_v_e_r_r_i_d_e<_/a>"

    should read:

    "@O_v_e_r_r_i_d_e"

    minus the underscores There is a bug in this forum that replaces the word o-v-e-r-r-i-d-e with links

  • @chrios1001 You are right about override. the solution is to add a space (' ') between @ and override. You can edit your post and make the change and you will see.

    Kf

  • edited May 2017

    @chrios1001 I tested this in Android302 and I have to make few changes:

    Add the following:

    @ Override 
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }
    

    And then these changes in the code, from :

    public boolean onCreateOptionsMenu(Menu menu) {
    //  AND
    return super.onCreateOptionsMenu(menu);
    

    Change to:

    void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //  AND
    super.onCreateOptionsMenu(menu, inflater);
    

    Kf

  • edited May 2017

    Thanks KF - I can't get this to compile with your additions (Android 3.3.3)

    error: The function "setHasOptionsMenu(boolean)" does not exist I have tried to find an example of this function by looking at Android code and copying in, but I have not got it to work yet.

    If it was working for you - can you post the full sketch including any additional functions you used?

  • edited June 2017

    Here is running code on Android Mode version 3.0.2 (From Add Mode manager)

    Kf

     //REFERENCE: https:// developer.android.com/guide/topics/ui/menus.html#options-menu  
    //============================================
    // IMPORTS:
    
    import android.app.Activity;
    import android.content.Context;
    import android.widget.FrameLayout;
    //import android.app.Fragment;
    
    import android.os.Environment;
    import android.graphics.Color;
    import android.widget.Toast;
    import android.os.Looper;
    import android.view.WindowManager;
    import android.os.Bundle;
    import android.view.ViewParent;
    import android.view.ViewGroup;
    import android.view.View;
    import android.widget.RelativeLayout;
    import android.view.LayoutInflater;
    import android.R.string;
    
    
    
    // Imports for displaying the menu
    
    import android.content.Intent;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.view.Menu;
    import android.view.MenuInflater; 
    import android.view.MenuItem;
    
    //===========================================================================
    // FINAL FIELDS:
    public static final int DREI_ID =  Menu.FIRST;
    public static final int VIER_ID =Menu.FIRST+2;
    public static final  int FUENF_ID = Menu.FIRST+3;
    public static final int SECHS_ID = Menu.FIRST+4;
    public static final int ACHT_ID = Menu.FIRST+5;
    
    //===========================================================================
    // GLOBAL VARIABLES:
    
    int corners = ACHT_ID;
    int angle;
    float x1, y1, x2, y2;
    
    Activity act;
    Context mC;
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void setup() {
      //size(400,600);
      fullScreen();
      orientation(PORTRAIT);
      //orientation(LANDSCAPE;)
    
      act = this.getActivity();
      Looper.prepare();
    
      textAlign(CENTER, CENTER);
      rectMode(CENTER);
    
      fill(255);
      strokeWeight(2);
      textSize(32);
    }
    
    void draw() {
      //background(0);
      fill(255, 20);
      rect(0, 0, width, height);
      polygon(corners, mouseX, mouseY, 10, 10);
    }
    
    void  polygon (int pages, int x, int y, int radiusX, int radiusY) {
      angle = (int) 360/pages;
      for (int grade=0; grade<360; grade+=angle) {
        x1 =sin(radians(grade))*radiusX+(x);
        y1 =cos(radians(grade))*radiusY+(y);
        x2 =sin(radians(grade+angle))*radiusX+(x);
        y2 =cos(radians(grade+angle))*radiusY+(y);
        line(x1, y1, x2, y2);
      }
    }
    
    void keyReleased() {
    }
    
    void mouseReleased() {
    }
    
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    @ Override
      void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // here the individual menu entries are generated
      menu.add(0, DREI_ID, Menu.NONE, "3");
      menu.add(1, VIER_ID, Menu.NONE, "4");
      menu.add(2, FUENF_ID, Menu.NONE, "5");
      menu.add(3, SECHS_ID, Menu.NONE, "6");
      menu.add(4, ACHT_ID, Menu.NONE, "8");
      super.onCreateOptionsMenu(menu, inflater);
    }
    
    // is called when a menu item has been selected
    @ Override
      boolean onOptionsItemSelected(MenuItem item) {
      // the selection is checked via the ItemId of the menu item
      switch (item.getItemId()) {
      case DREI_ID:
        corners=3;
        break;
    
      case VIER_ID:
        corners=4;
        break;
    
      case FUENF_ID:
        corners=5;
        break;
    
      case SECHS_ID:
        corners=6;
        break;
    
      case ACHT_ID:
        corners=8;
        break;
      }
      return super.onOptionsItemSelected(item);
    }
    
    //===========================================================================
    // ANDROID ACTIVITY LIFECYCLE'S FUNCTIONS:
    
    
    //  @@@@@@@@@@@@
    @ Override 
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }
    
  • I tried using the latest android mode version, 4.0-b7 and I am getting some errors calling any of the Menu calls. I won't be able to go any further today but probably tomorrow...

    Kf

  • Working for me on Android Mode 3.0.2! Thanks Kf - this is awesome. I'm hoping to get a full app bar working one day.

Sign In or Register to comment.