Android Options Menu

Well it is the third topic that I answer myself.
I guess I’m on my own now.
But I have received a lot of kind help on this forum so here is my contribution.
I assume that the code above of standard menu needs some kind of bar to be able to run properly.
In the code below I used a popup menu instead attached to a TextView. Clicking on the screen will enable or close the menu.
Download sketch here.

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.view.ViewGroup.LayoutParams;
import android.view.Gravity;
import android.R;
import android.widget.RelativeLayout;
import processing.android.CompatUtils;
import android.graphics.Color;
import android.view.WindowManager;

Activity act; 
Context mC;
FrameLayout fl;

int menuanchorId;
int menubarId;
boolean toggle;

void setup() {
  fullScreen();
  background(80, 80, 255);
  textAlign(CENTER);
  textSize(35);
  text("Click on screen to enable or close menu", width/2, height/2);
}

void draw() {
}

  public void onStart() {
  act = this.getActivity();
  mC= act.getApplicationContext();
  // To prevent status bar showing up
  act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

  TextView menubar = new TextView(act);
  menubar.setLayoutParams(new RelativeLayout.LayoutParams(width-width/12, width/12));
  menubar.setTextSize(20);
  menubar.setX(0);
  menubar.setY(0);
  menubar.setGravity(Gravity.CENTER);
  menubar.setBackgroundColor(Color.rgb(0, 0, 120));
  menubar.setTextColor(Color.WHITE);
  menubar.setText("My Popup Menu");
  menubar.setVisibility(View.GONE);
  menubarId=CompatUtils.getUniqueViewId();
  menubar.setId(menubarId);

  TextView menuanchor = new TextView(act);
  menuanchor.setLayoutParams(new RelativeLayout.LayoutParams(width/12, width/12));
  menuanchor.setTextSize(22);
  menuanchor.setTextColor(Color.WHITE);
  menuanchor.setX(width-width/12);
  menuanchor.setY(0);
  menuanchor.setBackgroundColor(Color.rgb(0, 0, 120));
  menuanchor.setGravity(Gravity.CENTER);
  menuanchor.setText("⋮");
  menuanchor.setVisibility(View.GONE);
  menuanchorId=CompatUtils.getUniqueViewId();
  menuanchor.setId(menuanchorId);
  menuanchor.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
      TextView menuanchor = (TextView) act.findViewById(menuanchorId);
      PopupMenu popupMenu = new PopupMenu(mC, menuanchor);
      popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, "Red");
      popupMenu.getMenu().add(Menu.NONE, 2, Menu.NONE, "Green");
      popupMenu.getMenu().add(Menu.NONE, 3, Menu.NONE, "Blue");
      popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
          public boolean onMenuItemClick(MenuItem item) {
          switch (item.getItemId()) {
          case 1:
            background(255, 0, 0);
            text("Click on screen to enable or close menu", width/2, height/2);
            return true;
          case 2:
            background(0, 255, 0);
            text("Click on screen to enable or close menu", width/2, height/2);
            return true;
          case 3:
            background(0, 0, 255);
            text("Click on screen to enable or close menu", width/2, height/2);
            return true;
          default:
            return false;
          }
        }
      }
      );
      popupMenu.show();
    }
  }
  );

  fl = (FrameLayout)act.findViewById(R.id.content);
  FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
  fl.addView(menubar);
  fl.addView(menuanchor);
}

void mousePressed() {
  act.runOnUiThread(
    new Runnable() {
    public void run() {
      TextView tv = (TextView)act.findViewById(menuanchorId);
      TextView tvv = (TextView)act.findViewById(menubarId);
      if (toggle) {
        tv.setVisibility(View.VISIBLE);
        tvv.setVisibility(View.VISIBLE);
      } else {
        tv.setVisibility(View.GONE);
        tvv.setVisibility(View.GONE);
      }
    }
  }
  );
  toggle =! toggle;
}
1 Like