Click here to Skip to main content
12,700,567 members (33,810 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked

Writing android gui using python(basic gui elements)

, 19 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
    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.


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.

Python Timer 

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. 

Create basic android GUI elements using python 

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);

2. Create a button, set text, and color 

Button = Service.ButtonClass._New(MyLayout);
Button.setText("hello button");
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");

4. Create an EditText 

MyEdit = Service.EditTextClass._New(MyLayout);

5. Create an RadioGroup 

MyRadioGroup = Service.RadioGroupClass._New(MyLayout);
def MyRadioGroup_onCheckedChanged(self,Event,objid) :
    Service.ToastClass._New().makeText("[MyRadioGroup] event on click is trigger"+objid,0).show();
MyRadioGroup.onCheckedChanged = MyRadioGroup_onCheckedChanged; 

6.    Create RadioButton in the group 

MyRadioButton1 = Service.RadioButtonClass._New(MyRadioGroup);
MyRadioButton2 = Service.RadioButtonClass._New(MyRadioGroup);

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); 


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


About the Author

China China
No Biography provided

You may also be interested in...

Comments and Discussions

Suggestionformatting and indentation Pin
Leonardo Paneque18-Apr-12 12:46
memberLeonardo Paneque18-Apr-12 12:46 
GeneralRe: formatting and indentation Pin
li970519-Apr-12 5:06
memberli970519-Apr-12 5:06 
SuggestionFormatting needed Pin
Mika Wendelius18-Apr-12 11:15
mvpMika Wendelius18-Apr-12 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 19 Apr 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid