Introduction
Let’s continue the topic. In this article, we give an example of basic Android GUI elements created and controlled using Python, which may be buttons, textviews, checkboxes, radiobuttons.
Python Timer
Before the topic, we discuss how to create a timer using python. Python script code runs in the thread context of Android main activity. Therefore, we cannot use local loop statement for example, “while
”, to wait for some actions, such as waiting for a response from the 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:
- Create an object.
- Define a timer function and assign to the object.
- Create an object’s timer.
The detailed code example is as follows:
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.
Create Basic Android GUI Elements using Python
For basic Android GUI elements, the corresponding cle class name are “ButtonClass
”, “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 wraps general use functions and events of the class. To see which ones are wrapped, please refer to the document wrapandroid.chm.
In order to place GUI elements on the screen, we should first 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 color
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