Friday, May 14, 2021

 Write once mobile applications: 

Introduction: Mobile applications were written once for Android and then rewritten for iPhones and expertise grew around each ecosystem while the business logic remained independent. This has now been addressed with write-once business logic that can be run platform-independent. This article describes the methods involved in writing an application using the Codename One framework, which facilitates the features required from the native stacks. 

Description:  

Codename One emphasizes the “write once, deploy everywhere” model of mobile application development. While the Android platform has become synonymous with Java development and Apple device development has with Objective C, developers struggle to port business logic to either platform. Instead, Codename One allows the application to be written in Java and released to multiple platforms including iOS.  The binaries appear native to the platform on which they are released so the experience for the end-user is seamless, and this works for them since they do not want to know if the application was written in one language or the other. A sample program is included below for reference. 

Conclusion: The application can be written once. It can be deployed everywhere by targeting the build for those platforms and writing installers. The setup for the environment might take several attempts due to dependencies but writing the application is easy once the environment is up and running. 

 

package com.shrinktext.precis; 


import static com.codename1.ui.CN.*; 

import com.codename1.ui.Button; 

import com.codename1.ui.Form; 

import com.codename1.ui.Dialog; 

import com.codename1.ui.Display; 

import com.codename1.ui.Label; 

import com.codename1.ui.plaf.UIManager; 

import com.codename1.ui.util.Resources; 

import com.codename1.io.Log; 

import com.codename1.ui.Toolbar; 

import com.codename1.ui.layouts.BoxLayout; 

import com.codename1.io.NetworkEvent; 

 

/** 

* This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose  

of building native mobile applications using Java. 

*/ 

public class Precis { 
 

private Form current; 

private Resources theme; 
 

public void init(Object context) { 

// use two network threads instead of one 

updateNetworkThreadCount(2); 

 

theme = UIManager.initFirstTheme("/theme"); 

 

// Enable Toolbar on all Forms by default 

Toolbar.setGlobalToolbar(true); 

 

// Pro only feature 

Log.bindCrashProtection(true); 

 

addNetworkErrorListener(err -> { 

// prevent the event from propagating 

err.consume(); 

if(err.getError() != null) { 

Log.e(err.getError()); 

} 

Log.sendLogAsync(); 

Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null); 

});  

} 

public void start() { 

if(current !null){ 

current.show(); 

return; 

} 

TextModeLayout tl = new TextModeLayout(3, 2); 

Form f = new Form("Text Summarization form", tl); 

 

TextComponent input = new TextComponent().label("Input").multiline(true); 

TextComponent output = new TextComponent().label("Output").multiline(true); 

 

f.add(tl.createConstraint().widthPercentage(60), input); 

f.add(tl.createConstraint().widthPercentage(40), output); 

f.add(tl.createConstraint().horizontalSpan(2), description); 

f.setEditOnShow(input.getField()); 

Button b = new Button("Summarize"); 

f.add(b); 

b.addActionListener((e) -> OnClickSummarize(b)); 

Button b = new Button("Reset"); 

f.add(b); 

b.addActionListener((e) -> OnClickReset(b)); 

f.show(); 

} 

:

}

No comments:

Post a Comment