# How to create button in Processing

**URL:** https://discourse.processing.org/t/how-to-create-button-in-processing/40766
**Category:** Processing for Android
**Created:** [January 31, 2023, 4:21pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766 "2023-01-31T16:21:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![OverLord](https://avatars.discourse-cdn.com/v4/letter/o/ecc23a/32.png) [@OverLord](https://discourse.processing.org/u/OverLord)
#### Post date: [January 31, 2023, 4:21pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/1 "2023-01-31T16:21:32Z")

</div>

How can use buttons in processing(java)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [January 31, 2023, 5:08pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/2 "2023-01-31T17:08:44Z")

</div>

Hi

Many ways to do that … Give more details what you want to achieve

---

<div class="post-metadata">

### Author: ![OverLord](https://avatars.discourse-cdn.com/v4/letter/o/ecc23a/32.png) [@OverLord](https://discourse.processing.org/u/OverLord)
#### Post date: [January 31, 2023, 5:22pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/3 "2023-01-31T17:22:41Z")

</div>

import processing.core.PApplet;  
import android.widget.Button;  
import android.widget.TextView;

public class Core extends PApplet {  
private Button button;  
public Core() {  
}

public void $CreateWindow(int width,  
int height,  
java.lang.String renderer,  
java.lang.String path) {  
}

public void $button(float x, float y, float w, float h) {

}  
}  
How to create button with class a Button?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [January 31, 2023, 5:33pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/4 "2023-01-31T17:33:21Z")

</div>

There is an example of Android widgets here: [https://github.com/vsquared/AndroidWidgets\_Processing4](https://github.com/vsquared/AndroidWidgets_Processing4)

Your original post was for ‘processing(java)’ but your code example is for Android widgets. The approaches are specific to each, so you should be more clear about which one you want.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 31, 2023, 9:37pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/5 "2023-01-31T21:37:33Z")

</div>

> [@OverLord](#):
>
> How to create button with class a Button?

for ‘processing(java)’

```auto
Button[] myButtons=new Button[10]; // make a button of type class Button ( just for ref )
int cmd = -1;

// ---------------------------------------------------------------------------------------

void setup() {
  size(768, 900);

  String[] names= {
    "moving block at the top",

    "1",
    "2",
    "3",

    "4",
    "5",
    "6",

    "7",
    "8",
    "9",
  };

  strokeWeight(3);
  textSize(14);
  for (int i=0; i < 10; i++) {
    //parameters: xi, yi, wi, hi, seli, visible, text, id
    myButtons[i] = new Button(600, 20 + i * 55,
      130, 50,
      false, true,
      names[i],
      i);
  }
}

void draw() {
  background(200, 200, 0);
  for (Button btn : myButtons) {
    btn.draw();
  }
  fill(0);
  text(cmd, 28, 28);
}

// ---------------------------------------------------------------------------------------

void mousePressed() {
  for (Button btn : myButtons) {
    btn.sel=false;
    if ( btn.myMousePressed()) {
      cmd = btn.id;
      btn.sel=true;
    }
  }//for
}

// ===================================================================================

class Button {
  int x, y,
    w, h,
    id;
  boolean sel, vis;
  String text;

  // constr
  Button(int x, int y,
    int w, int h,
    boolean sel, boolean vis,
    String atext,
    int id) {
    //
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.vis = vis;
    this.text = atext;
    this.id = id;
  }// constr

  void draw() {
    if (vis) {
      strokeWeight(3);

      if (sel) fill(0, 200, 0);
      else fill(0, 0, 200);

      if (over()) stroke(200, 0, 200);
      else stroke(0, 200, 200);

      rect(x, y,
        w, h);
      noStroke();
      fill(200);
      textSize(14);
      textAlign(LEFT, TOP);
      text(text,
        x + 4, y + 3,
        w-10, 900);
    }
  } // method

  boolean over() {
    return
      mouseX > x &
      mouseX < x + w &
      mouseY > y &
      mouseY < y + h;
  } // method

  boolean myMousePressed() {
    //called from main mousePressed
    return
      over();
  } // method
  //
} // class
//

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [January 31, 2023, 11:47pm UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/6 "2023-01-31T23:47:33Z")

</div>

Hi  
processing(java) or Android widgets ?  
For processing (java) here is More examples

> **[Button States](https://kdoore.gitbook.io/cs1335-java-and-processing/object-oriented-programming/button_states)**

> **[Buttons as Objects](https://kdoore.gitbook.io/cs1335-java-and-processing/object-oriented-programming/buttons_as_objects)**

> **[Button Class](https://kdoore.gitbook.io/cs1335-java-and-processing/object-oriented-programming/buttons_as_objects/button-class)**

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 1, 2023, 12:30am UTC](https://discourse.processing.org/t/how-to-create-button-in-processing/40766/7 "2023-02-01T00:30:21Z")

</div>

With @svan link and this link widgets could be more explained

> <https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/Button%20(native)>
