Introduction
Wrapandroid 0.8.5 is uploaded, programmers can download it from here. In this version, a method is presented which can be used to unzip files to directory on sdcard or package path. If path does not exist, it will be created automatically. The method name is “unzipAssetFile
”. Programmers can call it through activity object. The prototype of the functions is “boolean unzipAssetFile(StarObjectClass self,String zipFileName, String outputDirectory,Boolean OverWriteFlag)
”. The zipFileName
is located on assets directory of the project. From version 0.8.5, layout is supported. You can use “findViewById
” or “inflate
” function to retrieve widgets defined in XML file. The two functions are a little different from the Android version. “ClassName
” is added as an input parameter; because program does not know which class the widgets belong to. The classname is widget name with a “Class
” postfix. For example, LinearLayout
should be used as LinearLayoutClass
.
First Example
This example is shown how to get widgets defined in layout.
layout file main.xml, which includes a linearlayout, textview and a button
="1.0"="utf-8"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget73"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/widget74"
android:layout_width="220dp"
android:layout_height="48dp"
android:text="thank for your use"
android:typeface="serif"
android:textStyle="bold"
android:textColor="#ffff0000"
android:layout_x="284dp"
android:layout_y="220dp"
android:textSize="16dp"
/>
</LinearLayout>
Python Code
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
StarActivity = Service.ActivityClass.getCurrent();
MyText = StarActivity.findViewById("TextViewClass",StarActivity.getResource("id/widget45"));
MyText.setText("from layout");
MyButton = StarActivity.findViewById("ButtonClass",StarActivity.getResource("id/widget74"));
def MyButton_onClick(self, Ev) :
Service.ToastClass._New().makeText("Button is click", 0).show();
MyButton.onClick = MyButton_onClick;
MyButton.setOnClickListener();
MyButton.setText("click me");
MyLinearLayout = StarActivity.findViewById
("LinearLayoutClass",StarActivity.getResource("id/widget73"));
MyDynaButton = Service.ButtonClass._New(MyLinearLayout);
def MyDynaButton_onClick(self, Ev) :
Service.ToastClass._New().makeText("Button is click", 0).show();
MyDynaButton.onClick = MyDynaButton_onClick;
MyDynaButton.setOnClickListener();
MyDynaButton.setText("created dynamically");
MyDynaButton.setLinearLayoutParams(100,50);
Screenshot
Example (view holder)
Viewholder is often talked about and used in list view to speed up scroll process. Using Python, we can also use a method similar to the view holder. For python is a dynamic language, this is easy to realize.
Layout file, which will be used in list view
="1.0"="utf-8"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22px" />
<TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px" />
</LinearLayout>
<Button android:id="@+id/view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="more"
android:layout_gravity="bottom|right" />
</LinearLayout>
Python Code
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
StarActivity = Service.ActivityClass.getCurrent();
MyLayout = Service.AbsoluteLayoutClass._New(StarActivity);
mInflater = Service.LayoutInflaterClass._New();
MyAdapter = Service.BaseAdapterClass._New();
def MyAdapter_getCount(self) :
return 20;
MyAdapter.getCount = MyAdapter_getCount;
def MyAdapter_getItem(self,position) :
return position;
MyAdapter.getItem = MyAdapter_getItem;
def MyAdapter_getItemId(self,position) :
return position;
MyAdapter.getItemId = MyAdapter_getItemId;
def MyAdapter_getView(self,position,convertView,parent) :
global Service,mInflater,StarActivity;
if(convertView == None) :
holder={};
convertView = mInflater.inflate("LinearLayoutClass",
StarActivity.getResource("layout/vlist2"),None);
holder["img"] = convertView.findViewById
("ImageViewClass",StarActivity.getResource("id/img"));
holder["title"] = convertView.findViewById
("TextViewClass",StarActivity.getResource("id/title"));
holder["info"] = convertView.findViewById
("TextViewClass",StarActivity.getResource("id/info"));
holder["viewBtn"] = convertView.findViewById
("ButtonClass",StarActivity.getResource("id/view_btn"));
convertView.Tag = holder;
convertView._SLockGC();
else :
holder = convertView.Tag;
if( position % 3 == 0 ) :
holder["img"].setBackgroundResource(StarActivity.getResource("drawable/i1"));
holder["title"].setText("G1");
holder["info"].setText("google 1");
elif( position % 3 == 1 ):
holder["img"].setBackgroundResource(StarActivity.getResource("drawable/i2"));
holder["title"].setText("G2");
holder["info"].setText("google 2");
elif( position % 3 == 2 ):
holder["img"].setBackgroundResource(StarActivity.getResource("drawable/i3"));
holder["title"].setText("G3");
holder["info"].setText("google 3");
def holder_viewBtn_onClick(self, Ev) :
Service.ToastClass._New().makeText("Button is click", 0).show();
holder["viewBtn"].onClick = holder_viewBtn_onClick;
holder["viewBtn"].setOnClickListener();
return convertView;
MyAdapter.getView = MyAdapter_getView;
MyListView = Service.ListViewClass._New(MyLayout);
MyListView.setAbsoluteLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT,0,0);
MyListView.setAdapter(MyAdapter);
Screenshot
The example can be downloaded from here.