1 announcements homework #2 due feb 7 at 1:30pm submit the entire eclipse project in blackboard...

39
1 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

Upload: gwendolyn-flowers

Post on 30-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 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

1

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

Page 2: 1 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

2

Schedule

• Last time: Android basics

• Today: Android application development

• Thursday: Testing (B&B chapter 25)

• Next week: More testing!! (B&B chapters 26-28)

Page 3: 1 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

3

Page 4: 1 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

4

Page 5: 1 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

5

Android Forms

Page 6: 1 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

6

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

Page 7: 1 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

7

EditText:“description”

EditText:“title”

Button:“clear”

Button:“save” Spinner:

“spinner”

TextView

TextView

Page 8: 1 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

8

Page 9: 1 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

9

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>

Page 10: 1 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

10

23 <TextView android:layout_width="wrap_content"24 android:layout_height="wrap_content" 25 android:text="@string/description" />262728 <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" />333435 <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" />

Page 11: 1 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

11

40 <LinearLayout android:orientation="horizontal"41 android:layout_width="fill_parent"42 android:layout_height="wrap_content">4344 <Button android:id="@+id/save" 45 android:text="@string/save"46 android:layout_width="wrap_content"47 android:layout_height="wrap_content" />4849 <Button android:id="@+id/clear" 50 android:text="@string/clear"51 android:layout_width="wrap_content"52 android:layout_height="wrap_content" />5354 </LinearLayout>555657 </LinearLayout>

Page 12: 1 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

12

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

Page 13: 1 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

13

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

Button:“clear”

EditText:“description”

EditText:“title”

Page 14: 1 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

14

In main.xml...

40 <LinearLayout android:orientation="horizontal"41 android:layout_width="fill_parent"42 android:layout_height="wrap_content">4344 <Button android:id="@+id/save" 45 android:text="@string/save"46 android:layout_width="wrap_content"47 android:layout_height="wrap_content" />4849 <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" />5455 </LinearLayout>

Page 15: 1 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

15

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); 910 // clear it11 title.setText("");12 13 // same for the “description” field14 EditText desc =15 (EditText)findViewById(R.id.description);1617 desc.setText("");18 }1920 }

Page 16: 1 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

16

Applications withMultiple Activities

Page 17: 1 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

17

Page 18: 1 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

18

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

Page 19: 1 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

19

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>

Page 20: 1 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

20

LaunchActivity

ButtonClickActivity

Page 21: 1 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

21

In the LaunchActivity class...

1 // request code used in creating the new Activity 2 public static final int ButtonClickActivity_ID = 1; 3 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.launch); 7 } 8 9 public void onLaunchButtonClick(View v) {10 // create an Intent using the current Activity 11 // and the Class to be created12 Intent i = new Intent(this, ButtonClickActivity.class);13 14 // pass the Intent to the Activity, 15 // using the specified request code16 startActivityForResult(i, ButtonClickActivity_ID);17 }

Page 22: 1 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

22

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)

Page 23: 1 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

23

Page 24: 1 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

24

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 Activity10 finish(); 11 }

Page 25: 1 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

25

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()

Page 26: 1 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

26

Page 27: 1 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

27

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 object10 Integer clicks = 11 (Integer)(intent.getExtras().get(“NUM_CLICKS"));12 13 // display the pop-up14 Toast.makeText(15 this, 16 “Num clicks is " + clicks, 17 Toast.LENGTH_LONG)18 .show();19 20 break;21 } 22 }

Page 28: 1 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

28

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”

Page 29: 1 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

29

Activity lifecycle

Page 30: 1 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

30

Page 31: 1 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

31

Words of Wisdom

In general, an Activity should be as self-contained as possibleE.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 toLook out for null values in the Intent object in the

onActivityResult method

Page 32: 1 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

32

Android Threads

Page 33: 1 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

33

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 modify UI elements!

Page 34: 1 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

34

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

Page 35: 1 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

35

1 // this method gets when some button is clicked 2 public void onButtonClick(View view) { 3 new BackgroundTask().execute(editText.getText().toString()); 4 } 5 6 // class that will run in the background 7 // <Parameters, Progress, Result> 8 class BackgroundTask extends AsyncTask<String, Void, String> { 9 10 // automatically called by “execute”11 protected String doInBackground(String... inputs) {12 String reply = // do some background stuff...13 return reply; // this gets sent to onPostExecute14 }1516 // automatically called when doInBackground is done17 protected void onPostExecute(String result) {18 // update Views in the UI 19 tv = (TextView)findViewById(R.id.display_view);20 tv.setText(result);21 }22 }

Page 36: 1 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

36

“Toast” Notifications

Page 37: 1 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

37

Page 38: 1 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

38

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 message10 Toast.makeText(getApplicationContext(),11 R.string.welcome_message,12 Toast.LENGTH_LONG).show();13 14 }

Page 39: 1 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

39

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