srplab's blog

Discuss about multi language programming of c/c ,java,c#,python,etc. Distribute object middleware

Post sort order : index

7/28 POSTS
preview
7-9/28 POSTS

close

Writing android gui using lua(introduction)

Apr 27, 2012 11:51 PMPublicPageviews 425 0

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 series of articles to further explain how to programming android applications using lua.

Preparing environment


Unlike python, for lua engine has been embedded in CLE, you do not need to install other support packages.
a: 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

b: 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

a. Open eclipse, create a new android project, for example, “introduction”
b. 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>
c. copy files : starcore_android_r5.jar and wrapandroid.jar into the project directory, and add them to java build path, as shown below.

d. copy file : SRPWrapAndroidEngine.xml to assets directory.
e. edit IntroductionActivity.java
import com.srplab.wrapandroid.*;
public class IntroductionActivity extends WrapAndroidActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        StarActivity._Call("DoAssetsFile", "lua", "code.lua");
    }
}
f. 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();
The screenshot is as shown below:

Example can be downloaded from http://www.srplab.com/android/luagui_introduction.rar

Report abuse for this article

Copyright © 2013 Yahoo!, Inc. All rights reserved