65.9K
CodeProject is changing. Read more.
Home

Writing an Android GUI using Python (Gallery)

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Apr 24, 2012

CPOL

1 min read

viewsIcon

17443

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:

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:
    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:
    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:
    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

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