Click here to Skip to main content
15,887,746 members
Articles / Mobile Apps / Android

Writing an Android GUI using Python (Gallery)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Apr 2012CPOL1 min read 17K   2   3
Writing an Android GUI using Python

Introduction

Wrapandroid version is updated to 0.8.2. Please use this version.

We might write Python code outside eclipse. In this case, we have no logcat window to show print message from python. Do not worry about, cle supports syslog. The message can be output to syslog server. Using the following sentence to open syslog client:

Python
SrvGroup._SetOutputPort("192.168.0.129",514)

The IP address should be adjusted based on your environment.

The output looks like this:

Android gallery is a little more complicate gui element. In order to use gallery, we should first create an adapter, override its functions. And then, gallery object uses the adapter to obtain the content to be drawn.

Create Adapter Object

Wrapandroid’s adapterClass encapsulates Android Java class adapter. It has functions that can be overridden, such as getCount, getItem, getItemId, getView,etc. For a detailed explanation of these functions, please refer to Android documents.
  1. Create bitmap object:
    Python
    Bitmap0 = StarActivity.getBitmapDrawable
    (StarActivity.getResource("drawable/aqua02")).getBitmap();
    Bitmap1 = StarActivity.getBitmapDrawable
    (StarActivity.getResource("drawable/aqua03")).getBitmap();
    Bitmap2 = StarActivity.getBitmapDrawable
    (StarActivity.getResource("drawable/aqua04")).getBitmap();
    Bitmap3 = StarActivity.getBitmapDrawable
    (StarActivity.getResource("drawable/aqua05")).getBitmap();

    Bitmap resources locate in project directory res\drawable-hdpi. Python function "getBitmapDrawable" can be used to create bitmapdrawable object located in resources.

  2. getCount, getItem, and getItemId function:
    Python
    MyAdapter = Service.AdapterClass._New()
    def MyAdapter_getCount(self) :
        return 4;         //#the number of bitmapdrawables.
    MyAdapter.getCount = MyAdapter_getCount;  
    def MyAdapter_getItem(self,position) :
        return position;  //#position of bitmapdrawables
    MyAdapter.getItem = MyAdapter_getItem;  
    def MyAdapter_getItemId(self,position)  :
        return position;  //#id of bitmapdrawables
    MyAdapter.getItemId = MyAdapter_getItemId; 
  3. getView function:
    Python
    def MyAdapter_getView(self,position,convertView,parent) :
        global Service;
        i = Service.ImageViewClass._New();    //#create imageview object, 
               //and set its bitmap based on the position. The bitmaps are created in step 1.
        if( position == 0 ) :
            i.setImageBitmap(Bitmap0);
        if( position == 1 ) :
            i.setImageBitmap(Bitmap1);
        if( position == 2 ) :
            i.setImageBitmap(Bitmap2);
        if( position == 3 ) :
            i.setImageBitmap(Bitmap3);  
        i.setGalleryLayoutParams(Service.FILL_PARENT,
              Service.FILL_PARENT); //#set layout parameter of imageview
        i.setScaleType("FIT_XY");
        i.setBackgroundResource(StarActivity.getResource("mGalleryItemBackground"));
        i._LockGC();  //#call CLE function "_LockGC", which prevents imageview from python GC.
        return i;    
    MyAdapter.getView = MyAdapter_getView; 

Because imageview object is created by python, when function returns. The object will be freed by GC. So, we should call _LockGC before the function return.

Create Gallery Object

Python
MyGallery = Service.GalleryClass._New(MyLayout)  //#create gallery object
def MyGallery_onItemClick
(self,event,objid,position,id ):  //#create onItemClick event listener.
    Service.ToastClass._New().makeText
    ("[MyGallery] event on onItemClick is trigger "+
     objid+str(position)+str(id),0).show();
MyGallery.onItemClick = MyGallery_onItemClick;
MyGallery.setAdapter(MyAdapter);   //#set adapter of gallery object.
MyGallery.setLinearLayoutParams
(Service.FILL_PARENT,Service.FILL_PARENT);  //#set layout parameter 
                                            //of gallery 

The above codes and functions are simple. Detailed explanation can be found in the document of Android SDK.

Screenshot

This article was originally posted at http://blog.yahoo.com/srplab/articles/636595

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

 
QuestionAndroid Pin
Manov rao24-Apr-12 19:49
Manov rao24-Apr-12 19:49 
AnswerRe: Android Pin
li970524-Apr-12 22:23
li970524-Apr-12 22:23 
QuestionNeeds some work Pin
Richard MacCutchan24-Apr-12 3:12
mveRichard MacCutchan24-Apr-12 3:12 

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.