controlP5 Button solution for android / adpe

Here a solution for the weird behavior of the buttons from controlP5 on Android/adpe where you have to click twice to actually click the button.

This solution works with a single click and also with sliding the finger onto the Button and releasing.

The Button highlight is still a problem since if have no clue how to use the setColor() for the Button.
I’d appreciate any help on that one…
edit: figured out how to use the clor optionand added that to the first button

But when using an image for the button its not visible :wink:

here’s the code:


/*
  controlP5 button workaround for android/adpe
  by AmyS3
*/

import controlP5.*;

ControlP5 cp5;
CallbackListener cb;
String pbp=null;// we use those tho hold the button name
String pbp2=null;
color btncolor = color(#123456);

void setup()
{
  fullScreen();
  background(255);

  cp5 = new ControlP5(this);
  // make a few buttons to test
  cp5.addButton("b1")// that's the name we use to know which button is pressed
    .setCaptionLabel("Button 1")
    .setPosition(100, 100)
    .setSize(200, 50)
    .setColorBackground(btncolor)
    .setColorForeground(btncolor)
    .setColorActive(btncolor)
    ;

  cp5.addButton("b2")
    .setCaptionLabel("Button 2")
    .setPosition(100, 160)
    .setSize(200, 50)
    ;

  cp5.addButton("b3")
    .setCaptionLabel("Button 3")
    .setPosition(100, 220)
    .setSize(200, 50)
    ;

  //create a custom callbacklistener
  cb = new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_ENTER):
        // here we get the Button name when sliding in, or first click on this button
        pbp=theEvent.getController().getName();
        break;
        case(ControlP5.ACTION_CLICK):
        pbp2=theEvent.getController().getName();
        if (pbp==null)
        {// this gets fired when clicking the same button twice
          click(pbp2);
        }
        break;
        case(ControlP5.ACTION_LEAVE):
        // clear the pbp when sliding out, so we don't click outside the button
        pbp=null;
        break;
      }
    }
  };
  // add the above callback to controlP5
  cp5.addCallback(cb);
}

void draw()
{
  // this is for a first click or sliding in
  // and it needs to be in the draw() or it won't work
  // when using the mouseReleased() its not working
  if (!mousePressed&&pbp!=null)
  {
    click(pbp);
  }
}

void click(String btn)
{
  // here you can put your actual clicking code
  // you get the button name (eg. b1) and use that to discern
  // with if's or switch case.. etc.
/*
  for example:
  if(btn=="b1")
  {
     awesomefunction()
  }
*/
  pbp=null;// clear the name holder's
  pbp2=null;
  println(btn);// and action...
}




If I recall correctly there have been problems with using ControlP5 controls with Android. A couple of references follow for controls that were specifically made for Android:

https://github.com/vsquared/AndroidWidgets_Processing4

https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android

Yes, ControlP5 has issues, thats why i posted my workaround in case someone is interested.

i had a quick a look at the two other options that you posted and they look nice.
thanks for that!

but they seem to miss one feature that i really like from cP5… the “group” option, to quickly hide/toggle between groups of controlls. very usefull for multipaged ui’s.

Hi

Look at this

i found this one too after searching due to the controlP5 bugs, but skatolo is not working on adpe.

it does not compile.
apde is complaining:
skatolo-dex.jar is mentioned in export.txt,
but it’sa big fat lie and does not exist.

hence my workaround for cp5 which does compile on adpe.
maybe skatalo works on processing for the pc, but i only have a smartphone and no pc…

edit: nevermind… i just managed to dex the library and it works now. which is great and embarrassing at the same time…

so thank you for somehow pushing me to try skatalo again… :wink: