Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / Python

Writing Android GUI using Python (List view and Custom view)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Apr 2012CPOL 16.3K   1  
Writing Android GUI using Python (list view and custom view)

Introduction

The examples in this article will create a listview and a custom view. Android listview often uses adapter such as "ArrayAdapter<String>(…)". The syntax is Java generic, which may have no corresponding types in dynamic script languages, such as Python, Lua, etc. Therefore, we have to give specific class, such as StringArrayAdaper, for it can be used in these dynamic languages. For StringArrayAdapter, we can override function getView to return little more complex view for listview. For custom view, we can create instance of View, and override its function onDraw. In function onDraw, we may draw text or bitmaps using function of canvas.

List View

We first create an instance of StringArrayAdapterClass, which is defined wrapandroid.jar package. And then create a list view.

Create StringArrayAdapter and Override Its Function getView

Java
//#create instance of StringArrayAdapterClass
MyStringArrayAdapter = Service.StringArrayAdapterClass._New();
//#override function getView
def MyStringArrayAdapter_getView(self,position, convertView, parent) :
    global Service;
    
    //#create linearlayout
    i = Service.LinearLayoutClass._New();
    px = i.dp2px(5);
    //#set padding
    i.setPadding(px,px,px,px);
    //#set layout parameters, here we should uses function "setAbsListViewLayoutParams"
    i.setAbsListViewLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);
    //#create an imageview
    imageView = Service.ImageViewClass._New(i);
    //#set padding
    imageView.setPadding(5,5,5,5);
    //#set layout parameters. Because imageView is child of LinearLayout, 
    //we should use function "setLinearLayoutParams".
    imageView.setLinearLayoutParams(i.dp2px(24),i.dp2px(24));
    //#create TextView
    itextview = Service.TextViewClass._New(i);
    //#set layout parameter
    itextview.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);
    //#set size of font.
    itextview.setTextSize(i.sp2px(18))
    //#set content of textview and imageview based on position
    if( position == 0 ) :
        itextview.setText("Android");
        imageView.setImageResource(StarActivity.getResource("drawable/android_logo"));
    if( position == 1 ) :
        itextview.setText("WindowsMobile");
        imageView.setImageResource(StarActivity.getResource("drawable/windowsmobile_logo"));
    if( position == 2 ) :
        itextview.setText("iOS");
        imageView.setImageResource(StarActivity.getResource("drawable/ios_logo"));
    if( position == 3 ) :
        itextview.setText("Blackberry");
        imageView.setImageResource(StarActivity.getResource("drawable/blackberry_logo"));
    //#call _LockGC function before function returns to prevent the object GC by python.
    i._LockGC();
    return i;
MyStringArrayAdapter.getView = MyStringArrayAdapter_getView;    

#add string values to StringArrayAdapter.
MyStringArrayAdapter.add("Android");
MyStringArrayAdapter.add("WindowsMobile");
MyStringArrayAdapter.add("iOS");
MyStringArrayAdapter.add("Blackberry");

Create ListView and Set Its Adapter

Java
//#create ListView
MyListView = Service.ListViewClass._New(MyLayout);
//#set its onItemClick event listener.
def MyListView_onItemClick(self, Ev,objid,position,id) :
    Service.ToastClass._New().makeText("[MyListView] event on click is onItemClick "+objid,0).show();
MyListView.onItemClick = MyListView_onItemClick;    
//#set layout prameter
MyListView.setLinearLayoutParams(Service.FILL_PARENT,150);
//#set adapter
MyListView.setAdapter(MyStringArrayAdapter);

Custom View

For custom view, we can create instance of View, and override its function onDraw. In function onDraw, we may draw text or bitmaps using function of canvas.

Create a Paint and BitmapFactory for Using in Next Step

Java
MyPaint = Service.PaintClass._New();  
MyBitmapFactory = Service.BitmapFactoryClass._New();

Create View and Override Its Function onDraw

Java
def myView_onDraw(self,canvas) :
    global MyBitmapFactory,MyPaint
    
    //#call parent onDraw function
    self.onSuperDraw(canvas);    
    //#set color of Paint   
    MyPaint.setColor(0xFFFF0000);   
    //#draw a rect on canvas
    canvas.drawRect(10, 20, 100, 100, MyPaint); 
        
    //#get bitmap from resources.            
    MyBitmap = MyBitmapFactory.decodeResource
    (StarActivity.getResource("drawable/aqua02"));
    //#draw the bitmap on canvas
    canvas.drawBitmap(MyBitmap, 100, 100, None);                
    
    //#create an matrix object            
    matrix=Service.MatrixClass._New();
    //#set rotation parameter of matrix
    matrix.postScale(0.8, 0.8);
    matrix.postRotate(45);
    //create a new bitmap and set to the result of previous bitmap with matris
    dstbmp=Service.BitmapClass._New();
    dstbmp.createBitmap0
    (MyBitmap,0,0,MyBitmap.getWidth(),MyBitmap.getHeight(),matrix,True);
    //#draw the bitmap
    canvas.drawBitmap(dstbmp, 300, 100, None);
    
    //#free object created locally
    matrix._Free();
    dstbmp._Free();
    MyBitmap._Free();
myView.onDraw = myView_onDraw;    
//#set layout parameter.
myView.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);

Screenshot

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

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

 
-- There are no messages in this forum --