Lecture notes

Download Report

Transcript Lecture notes

Announcements
• Homework #2 due Feb 7 at 1:30pm
• Submit the entire Eclipse project in
Blackboard
• Please fill out the when2meets when your Project
Manager emails you! It makes the projects go by
much more smoothly
1
Schedule
• Last time: Android basics
• Today: Android application development
• Thursday: Testing (B&B chapter 25)
• Next week: More testing!! (B&B chapters 26-28)
2
3
4
Android Forms
5
Out-of-the-box View classes


Provide common UI functionality
Form elements: text area, button, radio button,
checkbox, dropdown list, etc.

Date and time pickers

Auto-complete

Can mostly be placed in Layout using main.xml
6
EditText:
“title”
TextView
TextView
EditText:
“description”
Button:
“save”
Button:
“clear”
Spinner:
“spinner”
7
8
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <LinearLayout
4 xmlns:android="http://schemas.android.com/apk/res/android"
5
android:orientation="vertical"
6
android:layout_width="fill_parent"
7
android:layout_height="fill_parent">
8
9
<LinearLayout android:orientation="horizontal"
10
android:layout_width="fill_parent"
11
android:layout_height="wrap_content">
12
13
<TextView android:layout_width="wrap_content"
14
android:layout_height="wrap_content"
15
android:text="@string/title" />
16
17
<EditText android:id="@+id/title"
18
android:layout_width="wrap_content"
19
android:layout_height="wrap_content"
20
android:layout_weight="1"/>
21
22
</LinearLayout>
9
23 <TextView android:layout_width="wrap_content"
24
android:layout_height="wrap_content"
25
android:text="@string/description" />
26
27
28
<EditText android:id="@+id/description"
29
android:layout_width="fill_parent"
30
android:layout_height="wrap_content"
31
android:layout_weight="1"
32
android:scrollbars="vertical" />
33
34
35
<Spinner
36
android:id="@+id/spinner"
37
android:layout_width="fill_parent"
38
android:layout_height="wrap_content"
39
android:prompt="@string/choose_event_type" />
10
40 <LinearLayout android:orientation="horizontal"
41
android:layout_width="fill_parent"
42
android:layout_height="wrap_content">
43
44
<Button android:id="@+id/save"
45
android:text="@string/save"
46
android:layout_width="wrap_content"
47
android:layout_height="wrap_content" />
48
49
<Button android:id="@+id/clear"
50
android:text="@string/clear"
51
android:layout_width="wrap_content"
52
android:layout_height="wrap_content" />
53
54
</LinearLayout>
55
56
57 </LinearLayout>
11
Handling user interaction



Recall that each View has an onTouchEvent method
that is automatically called by Android when the
user interacts with the View
In the Android View classes, Events are dispatched
to registered Listeners depending on the type of
action (click, key press, long click, etc.)
For Buttons, you can simply use the main.xml file to
specify the method used to handle clicks
12
EditText:
“title”
1. Edit the Layout (in main.xml) so that the
“Clear” Button's “onClick” attribute is set
to the onClearButtonClick method
2. Implement the onClearButtonClick
method in your class that extends Activity
3. That method will automatically be called
when the user clicks the “Clear” button
EditText:
“description”
Button:
“clear”
13
In main.xml...
40 <LinearLayout android:orientation="horizontal"
41
android:layout_width="fill_parent"
42
android:layout_height="wrap_content">
43
44
<Button android:id="@+id/save"
45
android:text="@string/save"
46
android:layout_width="wrap_content"
47
android:layout_height="wrap_content" />
48
49
<Button android:id="@+id/clear"
50
android:text="@string/clear"
51
android:layout_width="wrap_content"
52
android:layout_height="wrap_content"
53
android:onClick="onClearButtonClick" />
54
55 </LinearLayout>
14
In the class that extends Activity...
1 /*
2 * When the Clear button is clicked, this method
3 * gets called.
4 */
5 public void onClearButtonClick(View view) {
6
7
// get the “title” field by its ID
8
EditText title = (EditText)findViewById(R.id.title);
9
10
// clear it
11
title.setText("");
12
13
// same for the “description” field
14
EditText desc =
15
(EditText)findViewById(R.id.description);
16
17
desc.setText("");
18
}
19
20 }
15
Applications with
Multiple Activities
16
17
Intents



When a new Activity is started, an Intent object is
created and passed to that Activity
The Intent object contains information about what
the Activity is meant to do, and any data it needs in
order to do it
When the Launcher starts an application, it looks
for the Activity with the “MAIN” action
18
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest
3
xmlns:android="http://schemas.android.com/apk/res/android"
4
package="edu.upenn.cis542"
5
android:versionCode="1"
6
android:versionName="1.0">
7
8 <application android:icon="@drawable/icon"
9
android:label="@string/app_name">
10
11
<activity android:name=“LaunchActivity"
12
android:label="@string/app_name">
13
<intent-filter>
14
<action android:name="android.intent.action.MAIN" />
15
<category android:name="android.intent.category.LAUNCHER"/>
16
</intent-filter>
17
</activity>
18
19
<activity android:name=“ButtonClickActivity" />
20
21 </application>
22
23 </manifest>
19
LaunchActivity
ButtonClickActivity
20
In the LaunchActivity class...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// request code used in creating the new Activity
public static final int ButtonClickActivity_ID = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch);
}
public void onLaunchButtonClick(View v) {
// create an Intent using the current Activity
// and the Class to be created
Intent i = new Intent(this, ButtonClickActivity.class);
// pass the Intent to the Activity,
// using the specified request code
startActivityForResult(i, ButtonClickActivity_ID);
}
21
Review: Starting a new Activity



Create an Intent object with a reference to a
Context (this) and the Class that represents the
new Activity
Add any key/value pairs to the Intent's Bundle by
calling putExtra
Call startActivity or startActivityForResult and pass
the Intent and the request code (int)
22
23
In ButtonClickActivity...
1 public void onFinishButtonClick(View view) {
2
// create the Intent object to send BACK to the caller
3
Intent i = new Intent();
4
5
// put the number of clicks into the Intent
6
i.putExtra(“NUM_CLICKS", num_clicks);
7
setResult(RESULT_OK, i);
8
9
// ends this Activity
10
finish();
11 }
24
Review: Finishing an Activity




Create an Intent object (empty constructor)
Call putExtra with key/value pairs or putExtras with
a Bundle object (where key/val pairs were set with
putString)
Call setResult with the result code (usually either
RESULT_OK or RESULT_CANCELED) and the
Intent
Call finish()
25
26
In LaunchActivity...
1 // this method gets called when an Activity finishes
2 protected void onActivityResult(int requestCode,
3
int resultCode, Intent intent) {
4
super.onActivityResult(requestCode, resultCode, intent);
5
6
// the requestCode lets us know which Activity it was
7
switch(requestCode) {
8
case ButtonClickActivity_ID:
9
// get the number of clicks from the Intent object
10
Integer clicks =
11
(Integer)(intent.getExtras().get(“NUM_CLICKS"));
12
13
// display the pop-up
14
Toast.makeText(
15
this,
16
“Num clicks is " + clicks,
17
Toast.LENGTH_LONG)
18
.show();
19
20
break;
21
}
27
22 }
Review: Returning from an Activity



onActivityResult is called in the calling Activity, with
the request code, result code, and Intent object as
parameters
Use request code to figure out which Activity it is
that's returning (in case you created more than
one)
Use Intent object to get back any “return values”
28
Activity lifecycle
29
30
Words of Wisdom

In general, an Activity should be as self-contained
as possible


E.g., responsible for its own persistence, instead of
passing data along to the caller Activity
The user may click the “Back” button, and the callee
Activity may not finish the way you want it to

Look out for null values in the Intent object in the
onActivityResult method
31
Android Threads
32
Threads




Android will show an “ANR” error if a View does
not return from handling an event within 5 seconds
Or, if some code running in the “main thread”
prohibits UI events from being handled
This means that any long-running code should run
in a background thread
However, background threads are not allowed to
33
modify UI elements!
How Android threading works



Create a class that extends AsyncTask
To start the new thread, call the AsyncTask's
execute method
When execute is called, Android does the
following:
1. runs onPreExecute in the main (UI) thread
2. runs doInBackground in a background thread
3. runs onPostExecute in the main (UI) thread
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// this method gets when some button is clicked
public void onButtonClick(View view) {
new BackgroundTask().execute(editText.getText().toString());
}
// class that will run in the background
// <Parameters, Progress, Result>
class BackgroundTask extends AsyncTask<String, Void, String> {
// automatically
protected String
String reply =
return reply;
}
called by “execute”
doInBackground(String... inputs) {
// do some background stuff...
// this gets sent to onPostExecute
// automatically called when doInBackground is done
protected void onPostExecute(String result) {
// update Views in the UI
tv = (TextView)findViewById(R.id.display_view);
tv.setText(result);
}
}
35
“Toast” Notifications
36
37
In the Activity class...
1 public void onCreate(Bundle savedInstanceState) {
2
super.onCreated(savedInstanceState);
3
setContentView(R.layout.main);
4
5
// set the key listener for the title field
6
EditText title = (EditText)findViewById(R.id.title);
7
title.setOnKeyListener(new TabKeyListener());
8
9
// display a Toast notification with a welcome message
10
Toast.makeText(getApplicationContext(),
11
R.string.welcome_message,
12
Toast.LENGTH_LONG).show();
13
14 }
38
For More Information



Official documentation: developer.android.com
Feel free to use Piazza for Android-related
questions when working on your group projects
Also, please share any info you find that may be
helpful to others
39