Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 emails you! It makes the projects go by much more smoothly

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

3 3

4 4

5 5 Android Forms

6 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

7 7 EditText: “description” EditText: “title” Button: “clear” Button: “save” Spinner: “spinner” TextView

8 8

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

10 10 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" />

11 11 40 <LinearLayout android:orientation="horizontal" 41 android:layout_width="fill_parent" 42android: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 55 56 57

12 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

13 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”

14 14 In main.xml... 40<LinearLayout android:orientation="horizontal" 41android:layout_width="fill_parent" 42android:layout_height="wrap_content"> 43 44<Button android:id="@+id/save" 45 android:text="@string/save" 46android:layout_width="wrap_content" 47android:layout_height="wrap_content" /> 48 49<Button android:id="@+id/clear" 50 android:text="@string/clear" 51android:layout_width="wrap_content" 52android:layout_height="wrap_content" 53android:onClick="onClearButtonClick" /> 54 55

15 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); 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 }

16 16 Applications with Multiple Activities

17 17

18 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

19 19 AndroidManifest.xml 1 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 14 15 16 17 18 19 20 21 22 23

20 20 LaunchActivity ButtonClickActivity

21 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 created 12 Intent i = new Intent(this, ButtonClickActivity.class); 13 14 // pass the Intent to the Activity, 15 // using the specified request code 16 startActivityForResult(i, ButtonClickActivity_ID); 17 }

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

23 23

24 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 Activity 10 finish(); 11 }

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

26 26

27 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 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 } 22 }

28 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”

29 29 Activity lifecycle

30 30

31 31 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

32 32 Android Threads

33 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!

34 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

35 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 // 8 class BackgroundTask extends AsyncTask { 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 onPostExecute 14 } 15 16 // automatically called when doInBackground is done 17 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 }

36 36 “Toast” Notifications

37 37

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

39 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


Download ppt "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."

Similar presentations


Ads by Google