The following source code demonstrates a class to create a drop down list. It was designed for use on a tablet, but has not been tested on smaller devices.
// Drop Down List control parts => a.)display field, b.)arrow, c.)listItems
final int _displayX = 100;
final int _displayY = 120;
final int _displayW = 500;
final int _displayH = 70;
final int _itemH = 70;
final int _arrwX = _displayX + _displayW;
final int _arrwY = _displayY;
final int _arrwSize = _displayH;
boolean drop;
color BLUE = color(64,124,188);
color GREEN = color(0,126,0);
String[] fruit = {"Apples","Peaches","Oranges","Bananas","Pears"};
int[] _itemY;
int selectedItem = -1;
class List {
void press(float mx, float my){
// arrow touches
if((mx >= _arrwX) && (mx <= _arrwX+_arrwSize) && (my >= _arrwY) && (my <= _arrwY+_arrwSize)){
if(drop == true){
drop = false;
} else {
drop = true;
}
} // list touches
if (fruit.length > 0) {
for(int j = 0; j < fruit.length; j++){
if((mx >= _displayX) && (mx <= _displayX + _displayW) && (my >= _itemY[j] ) && (my <= _itemY[j] + _itemH)) {
selectedItem = j;
drop = false;
}
}
}
}
void displayFieldString(String str) {
fill(255); // display field background color
rect(_displayX,_displayY,_displayW,_displayH);
fill(0); // text color
textSize(42);
text(str, _displayX + 10, _displayY + 15, _displayW, _displayH);
}
void display(){
// display field
if(selectedItem == -1){
displayFieldString("Select fruit:");
} else {
displayFieldString(fruit[selectedItem]);
}
// arrow
fill(255); // arrow background color
rect(_arrwX, _arrwY, _arrwSize, _arrwSize);
fill(GREEN); // arrow color
triangle(_arrwX+5, _arrwY+5, _arrwX+_arrwSize-5, _arrwY+5, _arrwX+_arrwSize/2, _arrwY+_arrwSize-5);
// listItems
if (drop == true){
if (fruit.length > 0) {
// list items
_itemY = new int[fruit.length];
for(int j = 0; j < fruit.length; j++){
_itemY[j] = (_displayY + _displayH) + j*_itemH;
fill(255);
rect(_displayX,_itemY[j],_displayW,_itemH);
fill(0);
textSize(42);
text(fruit[j], _displayX + 10, _itemY[j] + 15, _displayW, _itemH);
}
}
}
}
}
List list;
void setup() {
fullScreen();
orientation(LANDSCAPE);
list = new List();
drop = false;
}
void draw() {
background(BLUE);
list.display();
}
void mousePressed(){
list.press(mouseX, mouseY);
}