Writing android gui using python(basic gui elements)
Let’s continue the topic. In this article, we gives example of basic android gui elements created and controlled using python, which may be buttons, textviews, checkboxs, radiobuttons.
Before the topic, we discuss how to create timer using python. Python script code runs in the thread context of android main activity. Therefore, we can not use local loop statement for example, “while”, to wait some actions, such as waiting response from remote server. In this case, a timer may be used. Based on the support of CLE, create a timer using python is simple, there are three steps:
1. create an object.
2. define a timer function and assign to the object.
3. create an object’s timer.
The detailed code example is as follow
timerobj = Service._New();
def timerobj _timerfunc(self,arg1,arg2) :
print(“timer is trigger”);
timerobj.timerfunc = timerobj _timerfunc;
timerobj._SetTimer(100, timerobj.timerfunc,0,0); # timer triggered per second.
For basic android gui elements, the corresponding cle class name are “ButtoClass”, “TextViewClass”, “EditClass”, “CheckBoxClass”, “RadioGroupClass”, “RadioButtonClass”. The naming rules is android java class name with suffix “Class”, which is easy to remember. Functions and events are similar to android class. So, for detailed information about the class, you can refer to android development kit documents. By now, wrapandroid project is in progress, it wrap general used functions and events of the class. Which are wrapped, please refer to document wrapandroid.chm.
In order to place gui elements on the screen, we first should create layout, which may by Linear Layout or Absolute Layout. In the example, we use Linear Layout.
1. Create LinearLayout
MyLayout = Service.LinearLayoutClass._New(StarActivity);
MyLayout.setOrientation("VERTICAL");
2. Create a button, set text, and textcolor
Button = Service.ButtonClass._New(MyLayout);
Button.setText("hello button");
Button.setTextColor(0xFF0000FF);
Button.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);
def Button_onClick(self,ev) :
Service.ToastClass._New().makeText("button is clicked",0).show();
Button.onClick = Button_onClick;
3. Create a textview
Text = Service.TextViewClass._New(MyLayout);
Text.setText("hello text");
Text.setTextColor(0xFFFF0000);
Text.setTextSize(30);
Text.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);
4. Create an EditText
MyEdit = Service.EditTextClass._New(MyLayout);
MyEdit.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);
5. Create an RadioGroup
MyRadioGroup = Service.RadioGroupClass._New(MyLayout);
MyRadioGroup.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);
def MyRadioGroup_onCheckedChanged(self,Event,objid) :
Service.ToastClass._New().makeText("[MyRadioGroup] event on click is trigger"+objid,0).show();
return;
MyRadioGroup.onCheckedChanged = MyRadioGroup_onCheckedChanged;
6. Create RadioButton in the group
MyRadioButton1 = Service.RadioButtonClass._New(MyRadioGroup);
MyRadioButton1.setText("RadioButton1");
MyRadioButton2 = Service.RadioButtonClass._New(MyRadioGroup);
MyRadioButton2.setText("RadioButton2");
7. Create a timer, in timer, we update content of textview.
Index = 1;
def Text_timerfunc(self,TimerID,arg1,arg2) :
global Index
self.setText("hello text "+str(Index));
Index = Index + 1;
Text.timerfunc = Text_timerfunc;
Text._SetTimer(1, Text.timerfunc,0,0);
Screenshot:

Examples download:
http://www.srplab.com/android/pythongui_basicguielements.rar
http://www.srplab.com/android/pythongui_basicguielements.rar
