Click here to Skip to main content
Click here to Skip to main content

Writing android gui using python(basic gui elements)

, 19 Apr 2012
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. Python script code runs in the thread context

Introduction 

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

License

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

About the Author

li9705

China China
No Biography provided

Comments and Discussions

 
Suggestionformatting and indentation PinmemberLeonardo Paneque18-Apr-12 11:46 
GeneralRe: formatting and indentation Pinmemberli970519-Apr-12 4:06 
SuggestionFormatting needed PinmvpMika Wendelius18-Apr-12 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 Apr 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid