Introduction
Lua is more like Python. Both are all dynamic languages, with little difference in syntax. Therefore, these series of articles are similar to "write android gui using python" series.
Using CLE and wrapandroid project, programmers can also write Android GUI applications with lua. CLE supports interaction between lua and java, gives a common interface for multiple programming languages. Wrapandroid
project encapsulates Android Java class with cle objects. This article is an introduction. There will have a series of articles to further explain how to program Android applications using lua.
Preparing the Environment
Unlike Python, for lua engine has been embedded in CLE, you do not need to install other support packages.
- CLE may install from network by application automatically, you need only include starcore_android_r5.jar in the project. The file is in starcore_devfiles_r5.zip, which can be download from http://code.google.com/p/cle-for-android.
Wrapandroid
has two files: wrapandroid.jar and SRPWrapAndroidEngine.xml, which can be download from http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_2.rar
Begin Programming
- Open eclipse, create a new Android project, for example, "introduction"
- Add Permission, which is used to download and install cle for the application:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- Copy files: starcore_android_r5.jar and wrapandroid.jar into the project directory, and add them to Java build path, as shown below:
- Copy file: SRPWrapAndroidEngine.xml to assets directory.
- Edit IntroductionActivity.java
import com.srplab.wrapandroid.*;
public class IntroductionActivity extends WrapAndroidActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StarActivity._Call("DoAssetsFile", "lua", "code.lua");
}
}
- Create new text file code.lua in assets directory.
code.lua
Get Current Service Maintained by cle
SrvGroup = libstarcore._GetSrvGroup()
Service = SrvGroup:_GetService("","")
Get Current Activity, Which is Created by wrapandroid at init Stage
StarActivity = Service.ActivityClass:getCurrent();
From now on, we can create gui elements. The first element should be layout element, which may be linear layout, absolute layout, etc. In this example, we create linear layout, which contains an edit text and buttons.
Create Root Layout
MyLayout = Service.LinearLayoutClass:_New(StarActivity);
MyLayout:setOrientation("VERTICAL");
Create Title Layout and GUI Elements
MyTitleLayout = Service.LinearLayoutClass:_New(MyLayout);
MyTitleLayout:setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);
UrlEdit = Service.EditTextClass:_New(MyTitleLayout);
UrlEdit:setText("http://www.google.com");
UrlEdit:setLinearLayoutParams(StarActivity:getWidth()-200,Service.WRAP_CONTENT);
GoButton = Service.ButtonClass:_New(MyTitleLayout);
GoButton:setText("go");
GoButton:setLinearLayoutParams(100,Service.FILL_PARENT);
function GoButton:onClick(ev)
MyWebView:loadUrl(UrlEdit:getText());
end
ExitButton = Service.ButtonClass:_New(MyTitleLayout);
ExitButton:setText("exit");
ExitButton:setLinearLayoutParams(100,Service.FILL_PARENT);
function ExitButton:onClick(ev)
StarActivity:exit(0);
end
Create Webview Layout and Webview Instance
MyTitleLayout1 = Service.LinearLayoutClass:_New(MyLayout);
MyTitleLayout1:setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);
MyWebView = Service.WebViewClass:_New(MyTitleLayout1)
MyWebView:setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);
MyWebSettings = MyWebView:getSettings();
MyWebSettings:setJavaScriptEnabled(true);
MyWebSettings:_Free();