Click here to Skip to main content
15,860,861 members
Articles / Mobile Apps / Android

Writing Android GUI Using LUA (Introduction)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Apr 2012CPOL2 min read 55.7K   2  
How to write Android GUI using LUA (introduction)

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.

  1. 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.
  2. 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:
    Java
    <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
    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

Java
SrvGroup = libstarcore._GetSrvGroup()
Service = SrvGroup:_GetService("","")

Get Current Activity, Which is Created by wrapandroid at init Stage

Java
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

Java
MyLayout = Service.LinearLayoutClass:_New(StarActivity);
MyLayout:setOrientation("VERTICAL");

Create Title Layout and GUI Elements

Java
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

Java
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();
This article was originally posted at http://blog.yahoo.com/srplab/articles/637698

License

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --