Documente Academic
Documente Profesional
Documente Cultură
Agenda
Who are we? Introducing Codename One Lets Build An Application Q&A
Couple Of Hackers
Shai Almog Chen Fishbein
Codename One allows Java developers to build true native apps for all mobile devices Its free & Open Source!
API
Designer Simulator
Plugin
Features
GPS Maps Camera Video Social Analytics Storage Network Ads L10n Push E-Mail SMS XML JSON Contacts and
more...
Lets Start!
Standard project
directory name
We select the
We have one
resource le
Main Class
Callback for
application lifecycle events operates the basics
Statemachine Base
Auto generated by the GUI builder Never touch this class Contains resource le boilerplate code Subclass of UIBuilder
Statemachine
Subclass of
StateMachineBase code should reside invoke methods in StatemachineBase and UIBuilder
Lets run the app & see what we have right now
Resource File
Data le containing
everything the app needs:
Screen designs Images Localization Data les Themes
Layout Managers
Layout managers dene a ow for
components within a Container managers can be nested
Flow Layout
Positions components Allows alignment of
naturally automatic line breaks
!
Box Layout
Positions components in a
line or a column
Grid Layout
Positions the components in
equal size within a xed grid of rows/columns
!
Table Layout
Similar to HTML tables Variable width columns Complex component sizing/spanning
Add Icon
Select Images->
Quick Add Multi Images image
Multi Image
Multi Images deliver
the right image for every given DPI
Remove the
Edit Texts
Double click every entry, remove the
text from the text elds. Set the rst button text to Capture Photo and the second button text to Add Task
@Override protected void onMain_CaptureButtonAction(final Component c, ActionEvent event) { Capture.capturePhoto(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { if(evt == null){ return; } String path = (String) evt.getSource(); photo = Image.createImage(path); ((Label)c).setIcon(photo.scaledWidth(Display.getInstance().getDisplayWidth() / 3)); c.getParent().getParent().animateLayout(400); } catch (Exception ex) { ex.printStackTrace(); }
});
Deep In
@Override protected void onMain_CaptureButtonAction(final Component c, ActionEvent event) { Capture.capturePhoto(new ActionListener() { public void actionPerformed(ActionEvent evt) { Standard event callback try { when the photo is if(evt == null){ return; captured or dismissed } String path = (String) evt.getSource(); if(ImageIO.getImageIO() != null) { String photoName = "" + System.currentTimeMillis(); java.io.OutputStream os = Storage.getInstance().createOutputStream(photoName); ImageIO.getImageIO().save(FileSystemStorage.getInstance().openInputStream(path), os, ImageIO.FORMAT_JPEG, imageWidth, imageHeight, 0.9f); os.close(); Image img = Image.createImage(Storage.getInstance().createInputStream(photoName)); photo = photoName; ((Label)c).setIcon(img); c.getParent().getParent().animateLayout(400); } catch (Exception ex) { Log.e(ex); } After making changes that might
} }); }
affect the layout of the UI we need to reow the UI manually. This allows the UI to be more performant.
We scale on the lesystem to avoid loading a VERY large image which will crash a typical phone
The Method
@Override protected void beforeMain(Form f) { imageWidth = icon.getWidth(); imageHeight = icon.getHeight(); Container tasksContainer = findTasksContainer(f); tasksContainer.removeAll(); if(todos == null) { todos = new Vector<Hashtable<String,String>>(); return; } for(Hashtable<String,String> entry : todos) { MultiButton mb = createEntry(entry); tasksContainer.addComponent(mb); } }
We fetch the resource le and extract the multiimage of the default icon so we can get the default icon size based on the design
Find methods are automatically generated into statemachine base when we save the resource le
todos = (Vector<Hashtable<String,String>>)Storage.getInstance().readObject("todos");
There is no need to revalidate since the form wasnt shown yet. We create a multi button for every stored todo
storage
Add A Task
Go back to the GUI builder and click
Action Event for the addTaskButton
Apple Certicate
You need a developer certicate from
Apple which requires that you pay them. There are instructions in our developer guide
On Device Demo
Learn More
Codename One - codenameone.com Blog - codenameone.blogspot.com Source Code - code.google.com/p/
codenameone
Q&A