Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Writing Android GUI using Python (Introduction)

0.00/5 (No votes)
16 Apr 2012CPOL1 min read 15.5K  
Writing Android GUI using Python (Introduction)

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.

  1. Preparing the environment.
    1. Install PythonForAndroid from http://code.google.com/p/android-scripting
    2. 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
    3. 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
  2. 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", "python", "code.py");
          }
      }
    6. Create new text file code.py in assets directory.
  3. code.py
    1. Get current service maintained by cle:
      Java
      SrvGroup = libstarpy._GetSrvGroup()
      Service = SrvGroup._GetService("","")
    2. 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.

    3. Create root layout:
      Java
      MyLayout = Service.LinearLayoutClass._New(StarActivity);
      MyLayout.setOrientation("VERTICAL");
    4. 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);
      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;
    5. 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();

The screenshot is as shown below:

Examples can be download here.

License

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