Click here to Skip to main content
15,886,519 members
Articles / Mobile Apps / Android

Writing an Android GUI using Python

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 May 2012CPOL1 min read 19.6K   9   2
The examples in this article will create a menu, scroll view and popup window.

Image 1

Introduction

The examples in this article will create menu, scroll view and popup window. For menu widget, programmer should override Android functions of activity, which are “onCreateOptionsMenu”, “onPrepareOptionsMenu”, “onOptionsItemSelected”. Using Python, the same functions should be defined and assigned to activity object. Popup window is often used to show information or perform some interactions with customer. Python permits define functions in another function, which is convenient to define widgets in popup window.

Scroll View

Creating scroll view is more directly and simple.

Create Scroll View Object

Python
MyScroll = Service.ScrollViewClass._New(MyLayout);
MyScroll.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);

Create Root Layout in the Scroll View

Python
MyScrollLayout = Service.LinearLayoutClass._New(MyScroll);
MyScrollLayout.setOrientation("VERTICAL");
MyScrollLayout.setFrameLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);

Create a Button, When Clicked, We Create A New Button in Scroll View

Python
MyButtonIndex = 1;
//#set onClick event listener
def MyButton_onClick(self,Ev) :
global MyButtonIndex;
//#Create a new button in scroll view
b = Service.ButtonClass._New(MyScrollLayout);
//#Set text content and color
    b.setText("Button" + str(MyButtonIndex));
    b.setTextColor(0xFFFF0000);   
    b.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);
    MyButtonIndex = MyButtonIndex + 1;
return;
//#assign event listener to object
MyButton.onClick = MyButton_onClick;

Menu

For menu, we should override functions of the Activity object which are onCreateOptionsMenu, onPrepareOptionsMenu, and onOptionsItemSelected. The activity object is obtained using code “StarActivity = Service.ActivityClass.getCurrent();”.

Override Function “onCreateOptionsMenu”

In this function, we create some menu items:

Python
def StarActivity_onCreateOptionsMenu(self,menu) :
    menu.add1(0, 1, 1, "open");
    menu.add1(0, 2, 2, "edit");
    menu.add1(0, 3, 3, "update");
    menu.add1(0, 4, 4, "clode");
    return True;
StarActivity.onCreateOptionsMenu = StarActivity_onCreateOptionsMenu;

Override Function “onPrepareOptionsMenu”

The function simply returns true to enable menu to show:

Python
def StarActivity_onPrepareOptionsMenu(self,menu) :
    return True;
StarActivity.onPrepareOptionsMenu = StarActivity_onPrepareOptionsMenu;

Override Function “onOptionsItemSelected”

Python
def StarActivity_onOptionsItemSelected(self,menuitem) :    
    id = menuitem.getItemId();
    if( id == 1 ) :
        self.setTitle("Open Text!");
    if( id == 2 ) :
        self.setTitle("Edit Text!");
    if( id == 3 ) :
        self.setTitle("Update Text!");
    if( id == 4 ) :
        self.setTitle("Close Text!");
    return True; 
StarActivity.onOptionsItemSelected = StarActivity_onOptionsItemSelected;

Popup Window

Popup window is often used to show information or perform some interactions with customer.

Create a Button, When Clicked, We Create a Popup Window

Python
OpenPopWindowButton = Service.ButtonClass._New(ButtonLayout);
OpenPopWindowButton.setText("Open popup window");
OpenPopWindowButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);  
def OpenPopWindowButton_onClick(self, Ev):
   …
    return;
OpenPopWindowButton.onClick = OpenPopWindowButton_onClick

In onClick Event Function, A Popup Window is Created

Because Python permits to define function in another function, we can create popup window and create functions for it.

Python
def OpenPopWindowButton_onClick(self, Ev):
    //#create popup window
    MyPopupWindow = Service.PopupWindowClass._New()
    //#set onDismiss event listener
    def MyPopupWindow_onDismiss(self, Ev):
        Service.ToastClass._New().makeText("PopupWindow is dismiss",0).show();
        return;
    MyPopupWindow.onDismiss = MyPopupWindow_onDismiss;
    
    //#create root layout for popup window
    PopupLayout = Service.LinearLayoutClass._New();
    PopupLayout.setBackgroundColor(0xFF0000FF) #--blue
    //#create a button
    MyButton = Service.ButtonClass._New(PopupLayout);
    def MyButton_onClick(self, Ev) :
        //#when is clicked, we show information and close the popup window
        Service.ToastClass._New().makeText("Button is click",0).show();
        MyPopupWindow.dismiss();
        return;
    MyButton.onClick = MyButton_onClick;
    MyButton.setText("CloseWindow");
    MyButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT); 
    //#assign layout to popup window.
   MyPopupWindow.setContentView(PopupLayout);
    MyPopupWindow.setWidth(200);
    MyPopupWindow.setHeight(200);
    MyPopupWindow.setFocusable(True);      
    MyPopupWindow.showAtLocation(self,17,0,0);
    //#prevent garbage collected by python.
    MyPopupWindow._LockGC();
    return;
This article was originally posted at http://blog.yahoo.com/srplab/articles/638322

License

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionWrong Categories Pin
Charles Oppermann7-May-12 8:23
Charles Oppermann7-May-12 8:23 
This is categorized as being "Windows" and "COM", neither of which apply. Please consider revising.
/* Charles Oppermann */
http://weblogs.asp.net/chuckop

GeneralRe: Wrong Categories Pin
li97058-May-12 20:46
li97058-May-12 20:46 

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.