Click here to Skip to main content
12,244,651 members (56,232 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

32K views
2 bookmarked
Posted

Writing Android GUI Using LUA (Introduction)

, 30 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
IntroductionLua 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 suppo

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

  1. Open eclipse, create a new android project, for example, "introduction"
  2. 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>
    
  3. copy files : starcore_android_r5.jar and wrapandroid.jar into the project directory, and add them to java build path, as shown below.
  4. copy file : SRPWrapAndroidEngine.xml to assets directory.
  5. 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");
        }
    }
  6. 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

li9705
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid