Skip to content

Instantly share code, notes, and snippets.

@codenameone
codenameone / StyleFontImageSample.java
Created February 28, 2016 10:10
Usage of the FontImage class that allows integrating icon fonts into Codename One
Form hi = new Form("Icon Font");
Font materialFont = FontImage.getMaterialDesignFont();
int size = Display.getInstance().convertToPixels(6, true);
materialFont = materialFont.derive(size, Font.STYLE_PLAIN);
Button myButton = new Button("Save");
myButton.setIcon(FontImage.create("\uE161", myButton.getUnselectedStyle(), materialFont));
hi.add(myButton);
hi.show();
@codenameone
codenameone / FixedFontImageSample.java
Created February 28, 2016 09:53
Usage of the FontImage class that allows integrating icon fonts into Codename One
Form hi = new Form("Icon Font");
Font materialFont = FontImage.getMaterialDesignFont();
int w = Display.getInstance().getDisplayWidth();
FontImage fntImage = FontImage.createFixed("\uE161", materialFont, 0xff0000, w, w);
hi.add(fntImage);
hi.show();
@codenameone
codenameone / GlasspaneSample.java
Created February 26, 2016 20:52
Simple sample of using the Codename One glass pane
Form hi = new Form("Glass Pane", new BoxLayout(BoxLayout.Y_AXIS));
Style s = UIManager.getInstance().getComponentStyle("Label");
s.setFgColor(0xff0000);
s.setBgTransparency(0);
Image warningImage = FontImage.createMaterial(FontImage.MATERIAL_WARNING, s).toImage();
TextField tf1 = new TextField("My Field");
tf1.getAllStyles().setMarginUnit(Style.UNIT_TYPE_DIPS);
tf1.getAllStyles().setMargin(5, 5, 5, 5);
hi.add(tf1);
hi.setGlassPane((g, rect) -> {
@codenameone
codenameone / TransitionsSample.java
Created February 23, 2016 19:27
Sample code for using the most common transition types in Codename One
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Transitions", new BoxLayout(BoxLayout.Y_AXIS));
Style bg = hi.getContentPane().getUnselectedStyle();
bg.setBgTransparency(255);
bg.setBgColor(0xff0000);
Button showTransition = new Button("Show");
Picker pick = new Picker();
pick.setStrings("Slide", "SlideFade", "Cover", "Uncover", "Fade", "Flip");
pick.setSelectedString("Slide");
TextField duration = new TextField("10000", "Duration", 6, TextArea.NUMERIC);
@codenameone
codenameone / FileStackMultipartUploadSample.java
Created February 22, 2016 03:48
Sample code for uploading to FileStack with the MultipartUpload class from Codename One
public void pictureUpload(final Callback<String> resultURL) {
String picture = Capture.capturePhoto(1024, -1);
if(picture!=null){
String filestack = "https://www.filestackapi.com/api/store/S3?key=MY_KEY&filename=myPicture.jpg";
MultipartRequest request = new MultipartRequest() {
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
Map<String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
String url = (String)result.get("url");
if(url == null) {
@codenameone
codenameone / ChartDemo.java
Created February 21, 2016 19:05
Simple code to show a PieChart using the Codename One ChartComponent
/**
* Creates a renderer for the specified colors.
*/
private DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[]{20, 30, 15, 0});
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
@codenameone
codenameone / SwipableContainerSample.java
Last active July 8, 2020 09:57
Shows the usage of the SwipableContainer that allows gesture swiping a UI in Codename One
public void showForm() {
Form hi = new Form("Swipe", new BoxLayout(BoxLayout.Y_AXIS));
hi.add(createRankWidget("A Game of Thrones", "1996")).
add(createRankWidget("A Clash Of Kings", "1998")).
add(createRankWidget("A Storm Of Swords", "2000")).
add(createRankWidget("A Feast For Crows", "2005")).
add(createRankWidget("A Dance With Dragons", "2011")).
add(createRankWidget("The Winds of Winter", "TBD")).
add(createRankWidget("A Dream of Spring", "TBD"));
hi.show();
@codenameone
codenameone / StarSlderSample.java
Last active July 8, 2020 09:57
Slider can be customized to show a 5 star picking UI in Codename One
public void showForm() {
Form hi = new Form("Star Slider", new BoxLayout(BoxLayout.Y_AXIS));
hi.add(FlowLayout.encloseCenter(createStarRankSlider()));
hi.show();
}
private void initStarRankStyle(Style s, Image star) {
s.setBackgroundType(Style.BACKGROUND_IMAGE_TILE_BOTH);
s.setBorder(Border.createEmpty());
s.setBgImage(star);
Form hi = new Form("ToastBarDemo", BoxLayout.y());
Button basic = new Button("Basic");
Button progress = new Button("Progress");
Button expires = new Button("Expires");
Button delayed = new Button("Delayed");
hi.add(basic).add(progress).add(expires).add(delayed);
basic.addActionListener(e -> {
ToastBar.Status status = ToastBar.getInstance().createStatus();
@codenameone
codenameone / ListSetRenderingPrototypeSample.java
Created February 18, 2016 20:36
Sample of the usage of setRendering prototype
Map<String, Object> proto = new HashMap<>();
map.put("Line1", "WWWWWWWWWWWWWWWWWWWW");
map.put("Line2", "WWWWWWWWWWWWWWWWWWWW");
int mm5 = Display.getInstance().convertToPixels(5, true);
map.put("icon", Image.create(mm5, mm5));
myMultiList.setRenderingPrototype(map);