PythonForAndroid provides support for Python script language on Android. CLE project supports interaction between Python and Java, gives a common interface for multiple programming languages. And wrapandroid project encapsulates Android Java class with cle objects. Using the three components, programmers can write android GUI programs with python directly. This article is an introduction. There will be a series of articles to further explain how to program Android applications using Python.
- Preparing the environment.
- Install PythonForAndroid from http://code.google.com/p/android-scripting
- 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 downloaded from http://code.google.com/p/cle-for-android
- Wrapandroid has two files: wrapandroid.jar and SRPWrapAndroidEngine.xml, which can be downloaded from http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_1.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", "python", "code.py");
}
}
- Create new text file code.py in assets directory.
- code.py
- Get current service maintained by cle:
SrvGroup = libstarpy._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);
def GoButton_onClick(self,ev) :
global MyWebView;
MyWebView.loadUrl(UrlEdit.getText());
GoButton.onClick = GoButton_onClick;
ExitButton = Service.ButtonClass._New(MyTitleLayout);
ExitButton.setText("exit");
ExitButton.setLinearLayoutParams(100,Service.FILL_PARENT);
def ExitButton_onClick(self,ev) :
global StarActivity;
StarActivity.exit(0);
ExitButton.onClick = ExitButton_onClick;
- 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:
Examples can be download
here.