Learn CODENAME-ONE with Real Code Examples
Updated Nov 23, 2025
Code Sample Descriptions
Codename One Simple Todo App
import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
public class TodoApp extends Form {
private DefaultListModel<String> todos = new DefaultListModel<>();
private List todoList;
private TextField newTodo;
public TodoApp() {
setTitle("Todo App");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
newTodo = new TextField("", "New Todo");
Button addButton = new Button("Add");
todoList = new List(todos);
addButton.addActionListener(e -> {
if(!newTodo.getText().trim().isEmpty()){
todos.addItem(newTodo.getText().trim());
newTodo.clear();
}
});
addAll(newTodo, addButton, todoList);
}
public void start() {
show();
}
}
Demonstrates a simple Codename One app with a Todo list, adding tasks, and displaying them using native UI components.
Codename One Counter App
import com.codename1.ui.*;
import com.codename1.ui.layouts.FlowLayout;
public class CounterApp extends Form {
private int count = 0;
private Label counterLabel;
public CounterApp() {
setTitle("Counter App");
setLayout(new FlowLayout(Component.CENTER));
counterLabel = new Label("0");
Button inc = new Button("+");
Button dec = new Button("-");
inc.addActionListener(e -> update(++count));
dec.addActionListener(e -> update(--count));
addAll(dec, counterLabel, inc);
}
private void update(int value) {
counterLabel.setText(String.valueOf(value));
revalidate();
}
}
Demonstrates a simple counter app with increment and decrement buttons in Codename One.
Codename One Login Form
import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
public class LoginApp extends Form {
public LoginApp() {
setTitle("Login");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
TextField username = new TextField("", "Username");
TextField password = new TextField("", "Password", 20, TextField.PASSWORD);
Button loginBtn = new Button("Login");
loginBtn.addActionListener(e -> Dialog.show("Info", "Welcome, " + username.getText(), "OK", null));
addAll(username, password, loginBtn);
}
}
Shows a simple login form with username and password fields using Codename One UI components.
Codename One Notes App
import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.Preferences;
public class NotesApp extends Form {
private TextArea noteArea;
public NotesApp() {
setTitle("Notes");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
noteArea = new TextArea(Preferences.get("note", ""));
Button save = new Button("Save");
save.addActionListener(e -> {
Preferences.set("note", noteArea.getText());
Dialog.show("Saved", "Note saved successfully!", "OK", null);
});
addAll(noteArea, save);
}
}
Demonstrates a basic notes app that saves and loads notes using Preferences API.
Codename One Stopwatch App
import com.codename1.ui.*;
import com.codename1.ui.layouts.FlowLayout;
public class StopwatchApp extends Form {
private int seconds = 0;
private Label timeLabel;
private com.codename1.ui.util.UITimer timer;
public StopwatchApp() {
setTitle("Stopwatch");
setLayout(new FlowLayout(Component.CENTER));
timeLabel = new Label("00:00");
Button start = new Button("Start");
Button stop = new Button("Stop");
Button reset = new Button("Reset");
timer = new com.codename1.ui.util.UITimer(() -> {
seconds++;
timeLabel.setText(String.format("%02d:%02d", seconds / 60, seconds % 60));
revalidate();
});
start.addActionListener(e -> timer.schedule(1000, true, this));
stop.addActionListener(e -> timer.cancel());
reset.addActionListener(e -> { seconds = 0; timeLabel.setText("00:00"); });
addAll(timeLabel, start, stop, reset);
}
}
Implements a simple stopwatch with start, stop, and reset using Codename One Timer.
Codename One Image Viewer
import com.codename1.ui.*;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.URLImage;
public class ImageViewerApp extends Form {
public ImageViewerApp() {
setTitle("Image Viewer");
setLayout(new BorderLayout());
Image placeholder = Image.createImage(100, 100);
URLImage img = URLImage.createToStorage(placeholder, "sample", "https://picsum.photos/200");
Label imageLabel = new Label(img);
add(BorderLayout.CENTER, imageLabel);
}
}
Loads and displays an image from URL using Codename One's URLImage.
Codename One Theme Switcher
import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
public class ThemeApp extends Form {
private boolean darkMode = false;
public ThemeApp() {
setTitle("Theme Switcher");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Button toggle = new Button("Toggle Theme");
Label label = new Label("Light Mode Active");
toggle.addActionListener(e -> {
darkMode = !darkMode;
UIManager.getInstance().setThemeProps(UIManager.getInstance().getTheme(darkMode ? "dark" : "light"));
label.setText(darkMode ? "Dark Mode Active" : "Light Mode Active");
revalidate();
});
addAll(label, toggle);
}
}
Demonstrates theme switching (light/dark mode) in Codename One.
Codename One Calculator App
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));
}
}
}
Implements a minimal calculator performing basic arithmetic in Codename One.
Codename One Weather Fetcher
import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.*;
public class WeatherApp extends Form {
public WeatherApp() {
setTitle("Weather App");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
TextField city = new TextField("", "Enter City");
Button fetch = new Button("Fetch");
Label result = new Label();
fetch.addActionListener(e -> {
ConnectionRequest req = new ConnectionRequest();
req.setUrl("https://api.weatherapi.com/v1/current.json?key=demo&q=" + city.getText());
req.addResponseListener(res -> {
String data = new String(req.getResponseData());
result.setText(data.substring(0, Math.min(100, data.length())) + "...");
revalidate();
});
NetworkManager.getInstance().addToQueue(req);
});
addAll(city, fetch, result);
}
}
Fetches weather data using a REST API and displays results in Codename One.
Codename One QR Scanner
import com.codename1.ui.*;
import com.codename1.media.*;
import java.io.IOException;
public class QRScannerApp extends Form {
public QRScannerApp() {
setTitle("QR Scanner");
setLayout(new BorderLayout());
Button scan = new Button("Scan QR");
Label result = new Label();
scan.addActionListener(e -> {
try {
Capture.capturePhoto((path) -> result.setText("Scanned file: " + path));
} catch (Exception ex) {
Dialog.show("Error", ex.getMessage(), "OK", null);
}
});
add(BorderLayout.CENTER, scan);
add(BorderLayout.SOUTH, result);
}
}
Demonstrates scanning a QR code using Codename One's built-in CameraKit.