Writing an Android GUI using Python (7:Activity)
Activity of android application is main entity which likes windows on win32 platform. It has lifecycle, contains gui widgets, and manages them together. Activity can create child activities, with initial parameters, and gets results from them. Here gives an example to operate activities, which code
Introduction
Activity of android application is main entity which likes windows on win32 platform. It has lifecycle, contains gui widgets, and manages them together. Activity can create child activities, with initial parameters, and gets results from them. Here gives an example to operate activities, which code is written with python based on wrapandroid project. The purpose of this article is to tell programmer how to operate activities using python, rather than explain activity’s lifecycle and how to create child activities, which is talked in detail in android sdk documents.
Examples given in this article is simple. We create two activities, one is root, and another is child. The root activity contains an edit widget, which is used to get input from user, and a button widget. When users press button, child activity is created with input as parameter. Child activity displays the parameter in text widgets, shows an edit widget for user to input result for parent. At last, the text returned from child is shown in parent activity.
Root Activity
Layout XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_gravity="center_vertical"
>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="input text :"
>
</TextView>
<EditText
android:id="@+id/widget38"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="18sp"
>
</EditText>
</LinearLayout>
<Button
android:id="@+id/widget39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send to child"
>
</Button>
<LinearLayout
android:id="@+id/widget40"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_gravity="center_vertical"
>
<TextView
android:id="@+id/widget41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="result from child :"
>
</TextView>
<TextView
android:id="@+id/widget42"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
>
</TextView>
</LinearLayout>
</LinearLayout>
Code of activity
public class ActivityActivity extends WrapAndroidActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StarActivity._Call("DoAssetsFile", "python", "code.py");
}
}
code.py
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
#--get activity
StarActivity = Service.ActivityClass.getCurrent();
ChildActivity
. It must be declared in AndroidManifest.xml. Otherwise, the call will be failed. AndroidManifest.xml: <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ActivityActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChildActivity" android:label="@string/app_name"/>
</application>
#--button
myEdit = StarActivity.findViewById("EditTextClass",StarActivity.getResource("id/widget38"));
myButton = StarActivity.findViewById("ButtonClass",StarActivity.getResource("id/widget39"));
def myButton_onClick(self, Ev) :
MyIntent = Service.IntentClass._New();
MyIntent.setClassName("ChildActivity");
MyIntent.putStringExtra("value",myEdit.getText());
StarActivity.startActivityForResult(MyIntent,1);
MyIntent._Free();
return;
myButton.onClick = myButton_onClick;
myButton.setOnClickListener();
#--receive result
myText = StarActivity.findViewById("EditTextClass",StarActivity.getResource("id/widget42"));
myText.setTextColor(0xFFFF0000)
def StarActivity_onActivityResult(self,requestCode, resultCode, data) :
if( requestCode == 1 and data != None ) :
myText.setText(data.getStringExtra("value"))
StarActivity.onActivityResult = StarActivity_onActivityResult;
Child Activity
Layout XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="from parent :"
>
</TextView>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="input text : "
>
</TextView>
<EditText
android:id="@+id/widget36"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:textSize="18sp"
>
</EditText>
</LinearLayout>
<Button
android:id="@+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="return to parent"
>
</Button>
</LinearLayout>
Code of child activity
code.py
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
//#--get activity, global python name space
ChildStarActivity = Service.ActivityClass.getCurrent();
First, we get start intent, which is set by parent. Then, obtains text view defined in the layout file, and show string of the intent in the text view. Gets button widget, set it’s onClick event listener. When the event is triggered, we build result intent for parent and call finish function to end the child activity.
//#--get intent which is created by parent
child_intent = ChildStarActivity.getIntent();
//#--get text view widget defined in layout
ChildText = ChildStarActivity.findViewById("TextViewClass",ChildStarActivity.getResource("id/widget33"));
//#--set value of intent to text view
ChildText.setText(child_intent.getStringExtra("value"))
//#--set text color
ChildText.setTextColor(0xFFFF0000)
//#--get EditText widgt
childEdit = ChildStarActivity.findViewById("EditTextClass",ChildStarActivity.getResource("id/widget36"));
//#--get Buttn defined in layout
childButton = ChildStarActivity.findViewById("ButtonClass",ChildStarActivity.getResource("id/widget37"));
//#--set onClick event listener
def childButton_onClick(self, Ev) :
//#--create a new intent
MyIntent = Service.IntentClass._New();
//#--set string value of edit to intent
MyIntent.putStringExtra("value",childEdit.getText());
//#--set intent as result of activity
ChildStarActivity.setResult1(0,MyIntent);
MyIntent._Free();
//#--end the activity
ChildStarActivity.finish();
return;
childButton.onClick = childButton_onClick;
childButton.setOnClickListener();
We also should capture “BACK” key event. When the key is pressed, we also end the activity.
def ChildStarActivity_onKeyDown(self,keyCode,event) :
if( keyCode == Service.AndroidConstantClass.getInt("KeyEvent","KEYCODE_BACK") ) :
//# create an intent object
MyIntent = Service.IntentClass._New();
//# set string value of intent object
MyIntent.putStringExtra("value","press key back");
//# set the intent as the result of child activity
ChildStarActivity.setResult1(0,MyIntent);
MyIntent._Free();
//# call "finish" to end the activity
ChildStarActivity.finish();
return True,True;
ChildStarActivity.onKeyDown = ChildStarActivity_onKeyDown
Screenshot
Parent Activity
Child Activity