Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Implements a minimal calculator performing basic arithmetic in Codename One.
import com.codename1.ui.*;
import com.codename1.ui.layouts.GridLayout;
public class CalculatorApp extends Form {
private TextField input;
private double current = 0;
private String op = "";
public CalculatorApp() {
setTitle("Calculator");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
input = new TextField();
Container grid = new Container(new GridLayout(4, 4));
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
for(String b : buttons) {
Button btn = new Button(b);
btn.addActionListener(e -> handleInput(b));
grid.add(btn);
}
addAll(input, grid);
}
private void handleInput(String val) {
if("0123456789.".contains(val)) input.setText(input.getText() + val);
else if("+-*/".contains(val)) { current = Double.parseDouble(input.getText()); op = val; input.clear(); }
else if(val.equals("=")) {
double second = Double.parseDouble(input.getText());
switch(op) {
case "+": current += second; break;
case "-": current -= second; break;
case "*": current *= second; break;
case "/": current /= second; break;
}
input.setText(String.valueOf(current));
}
}
}Codename One is a cross-platform mobile development framework that allows developers to build native mobile apps for iOS, Android, Windows, macOS, and web using Java or Kotlin. It provides a single codebase with a rich set of UI components and native device access.
Origin & Creator
Created by Shai Almog and the Codename One team in 2012, Codename One was designed to simplify mobile app development across platforms using Java.
Industrial Note
Best suited for teams with strong Java expertise who want to write once and deploy anywhere without managing multiple platform-specific projects.