Writing android gui using python(list view and custom view)
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.
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
//#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
//#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.
MyPaint = Service.PaintClass._New();
MyBitmapFactory = Service.BitmapFactoryClass._New();
Create View and override its function onDraw
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:

Examples download:
http://www.srplab.com/android/listandcustomview.rar
http://www.srplab.com/android/listandcustomview.rar
