abstract - university of nevada, web viewandroid.app.activity. even though you have created these...

20
IS 389 ANDROID MULTIPLE ACTIVITY LAB ABSTRACT In this lab, you will perform the following tasks: You will work with multiple activities and manage activities in the AndroidManifest.xml file. You will display log message and work with the LogCat class. You will also work with explicit intents so as to display these activities You will pass data to an activity. TASK 1: Creating the Application By now, you should be familiar with the process of creating an Android application, so I’ll provide minimal detail to create the basic application. Create a new Android using the following guidelines. 1. Name the application MultiActivityDemo. Name the package and application using the default options. 2. I suggest that you use Version 19 of the Android library. 1 | Page

Upload: phungminh

Post on 01-Mar-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

IS 389 ANDROID MULTIPLE ACTIVITY LABABSTRACTIn this lab, you will perform the following tasks:

You will work with multiple activities and manage activities in the AndroidManifest.xml file. You will display log message and work with the LogCat class. You will also work with explicit intents so as to display these activities You will pass data to an activity.

TASK 1: Creating the ApplicationBy now, you should be familiar with the process of creating an Android application, so I’ll provide minimal detail to create the basic application.

Create a new Android using the following guidelines.

1. Name the application MultiActivityDemo. Name the package and application using the default options.

2. I suggest that you use Version 19 of the Android library.

1 | P a g e

Page 2: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

3. Create a blank activity named MainActivity. Again layout names typically appear with the words reversed and an underscore between the word activity and corresponding layout name (main).

TASK 2: Adding Activities to an ApplicationIn this task, you will add a second activity to the application. Remember that an activity is a class. Therefore the process to add an activity is similar to the process of adding a Java Class. However, your activity should inherit from the android.app.Activity class.

1. In the Package Explorer, right-click on the package named MultiActivityDemo. In the context menu that appears, click New, and then click Class to begin creating a new class. The following

2 | P a g e

Page 3: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

dialog appears:

2. Use the default options for the Source folder and Package.

3. Set the name to Page2Activity as this will be the second page of the application. As you know, Android activities inherit from the class named android.app.Activity. To minimize the code you need to write, set the superclass to android.app.Activity . Make sure the Inherited abstract methods check box is checked. Click the Finish button to create the class. The code editor should be activated and the activity displayed as follows:

4. Now, create a third activity as you did in the previous steps. However, this time, name the activity Page3Activity. Again, make sure that the class inherits from android.app.Activity.

3 | P a g e

Page 4: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

Even though you have created these activities, they will not work or be compiled into the application. The reason is simple. They have not been added to the AndroidManifest.xml file. You can either use the wizard or you can edit the AndroidManifesst.xml file directly.

As you can see in the above figure, the Manifest tab supplies a graphical interface to edit general manifest attributes.

The Application tab allows you to edit settings specific to the application itself.

The last tab (.xml) allows you to edit the file directly.

On the Application tab shown in the following figure, you configure the application structure, and the activities that make up the application. Note that you do not need to change any of these settings.

As you can see in the above figure, the string and drawable folders are configured. If you scroll to the bottom of the dialog, it shows the activities that are included in the package.

4 | P a g e

Page 5: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

To add the activities to the AndroidManifest.xml file

1. In the Package Explorer, double-click on the AndroidManifest.xml file.

2. In the Manifest Editor, activate the Application tab.

3. Scroll to the bottom of the screen until you see the Application Nodes section. Note that your screen will not display any nodes yet.

4. Click the Add button. The following dialog box will appear:

5. Make sure the Activity list item is selected.

6. Click OK. The new activity is added to the program.

5 | P a g e

Page 6: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

7. Next you need to give it the correct name. In the right dialog box, titled Attributes for Activity, locate the Name property.

8. Click the Browse button to display the following dialog box:

9. Double-click Page2Activity, and Click the OK button. When complete, the activity should be renamed as follows. Note that you might need to switch tabs to refresh the screen (activity name)

6 | P a g e

Page 7: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

10. Repeat the preceding steps to add the activity named Page3Activity. When complete, your screen should look like the following:

11. When complete, check that the AndroidManifest.xml file looks like the following:

Of course, you could have just edited this by hand.

TASK 3: Creating Layouts (Views)In this task, you will create the layouts (views) for the three activities that you just created. These views will have the following characteristics:

You will create one view for each activity.

Each view will have two buttons.

Each button will display the corresponding activity. You will write the code to do this in the next task.

Each view will have TextView widgets.

Before you create the layouts, be aware of the following about layouts and text-based controls. So far, we have discussed relative and linear layouts Layouts seemed to cause some confusion so I will review them here.

7 | P a g e

Page 8: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

A LinearLayout causes all of its child controls to be aligned in a single direction (either horizontally or vertically. The layout direction is specified using the android:orientation attribute. The children are stacked one after the other. Using a vertical orientation, the controls are stacked one on top of each other such that there is one control per row. The width of the control is not relevant. Using a horizontal orientation, the list is one row high. A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

A RelativeLayout causes all of its child controls to be aligned relative to their siblings or relative to their parent. That is, siblings can be defined in terms of left-of another control or below another control. Control can also be aligned relative to the top (android:layout_alignParentTop) , left , center, or bottom of a parent view.

Note that there is much more to layouts than presented here.

To add a View to the package

At this point in your application’s development, one view was automatically created for you and associated with the default activity that was created when the package was created. However, no views yet exist for the other two activities. You will create those next.

1. In the Package Explorer, right-click on the package. From the context menu, click New, Other. 2. The following window appears. Here you can create various new Android objects, including a

layout file. Remember that an activity can have one or many views. Here, you are creating the view for the activity that you just created.

3. Select Android XML Layout File. Click Next to create the layout file.

8 | P a g e

Page 9: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

4. It is in this dialog box you create the view (layout) name. Typically the file name for a layout is the activity name reversed. Words appear in all lower-case characters and an underscore (_) separates each word. Set the name to activity_page2.xml.

5. Make sure that LinearLayout is selected.

6. Click Next. The wizard will create the template for a linear layout. The following dialog box appears:

9 | P a g e

Page 10: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

7. In this dialog box, you can further configure the layout. However, you will not do so here. Note that the folder is named /res/layout. This is the folder where all of the layouts are stored. Click Finish to create the activity and the following code:

As you can see from the above code segment, the layout for the view is a <LinearLayout> and the android:orientation is vertical meaning that each control will appear on its own line. The android:layout_width and android:layout_height are both set to match_parent meaning that the width and height will be the same size as the parent window.

Next, you will complete the user interface for the three activities by creating <TextView> and two horizontal buttons below the TextView. After you are complete, each view should look similar to the following:

10 | P a g e

Page 11: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

To create the user interface, complete the following steps:

1. Draw a <TextView> on the surface using the visual designer.

2. Then create a new <LinearLayout> (Horizontal) below the <TextView> that you just created.

3. Next, draw a Button inside of the <LinearLayout>.

4. Create a second button inside of the <LinearLayout>. It should appear to the right of the first button because you chose a horizontal <LinearLayout>.

5. The next step is not obvious. You need to rename the buttons from their default values (button1, button2) to something else. When you create the buttons on the second and third forms, they will be named button1 and button2 also. Thus, their IDs will not be unique and the application will not be wired correctly. The reason is simple, you are not really using unique IDs.

6. Rename buttons named button1 and button2 to button1Page2 and button2Page2, respectively. To do this, I suggest just editing the XML file. You can also use the viual designer and set properties manually.

When you are complete, the XML should look like the following:

11 | P a g e

Page 12: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

As you can see, the view uses an outermost <LinearLayout> laid out vertically. Thus, the <TextView> will appear on the first line (row). The inner <LinearLayout> laid out horizontally, contains two buttons. Thus, the two buttons will appear on the same row and next to each other.

To create the layout for the third view:

1. Next, create the layout for the third activity, just as you did for the second activity. Modify the name of the two buttons from button1 and button2 to button1Page3 and button1Page3. I have provided the following XML so that you can verify your code:

12 | P a g e

Page 13: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

2. Finally, modify the main activity (activity_main.xml) to use a <LinearLayout> instead of a <RelativeLayout>.

3. Create the <TextView> and <Button> widgets on the layout, as necessary.

4. Change the button names from button1 and button2 to button1Main and button2Main.

Again, the XML should be the same as the following:

13 | P a g e

Page 14: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

TASK 4: Starting an Activity

In this task, you will write the code that will execute when the user clicks each button. You will create the event listeners for each button. The code in the event listener will display the corresponding activity.

Note the following about this code.

The wizard created a template for the main activity. So the onCreate() and other callback procedures have been created for you.

The Activity named Page2Activity was created manually be you. It does not yet have any code in it though. Thus, you must create the callback procedures by hand.

The Activity named Page3Activity was created manually be you. It does not yet have any code in it though. Thus, you must create the callback procedures by hand.

To create the event handlers for the first activity:

1. Activate the code editor for the file named MainActivity.java.

14 | P a g e

Page 15: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

2. Modify the onCreate() callback procedure so that it contains the following code:

Note the following about the code that you just wrote. The statements are nearly identical to create the event listeners for the two buttons.

The superclass method is called as usual.

The setContentView method is called as usual to associate the model with the view.

The findViewById method gets a reference to the button. The button is named button1Main. Remember that this was the name that you chose for the button in the activity_main.xml file. Again, you get the reference through R.id. That same statement casts the return value to a button.

The next statement is the most complicated and creates the listener as you have seen before. Here however, the Intent is created as follows:

o The first argument to the Intent constructor contains a reference to the current activity (MainActivity.this).

o The second argument is the activity to be started (Page3Activity.class)

Finally, the startActivity runs the activity without any arguments. Data is not passed to the activity.

1. Activate the code editor for the file named Page2Activity.java.

2. Enter the following statements to create and register the callbacks that will switch activities to page 1 and page 3.

15 | P a g e

Page 16: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

3. Activate the code editor for the file named Page3Activity.java. This time, figure out the code yourself to switch activities to the main activity and activity 2.

TASK 5: Renaming the <TextView> controlsAt this point, in the application’s development, the <TextView> controls on the three layouts have the same name. So that you can uniquely identify each control, they should each have a unique name

1. In the file activity_main.xml change the name of the <TextView> control to textView1Main.

2. In the file activity_page2.xml, change the name of the <TextView> control to textView1Page2.

3. In the file activity_page3, change the name of the <TextView> control to textView1Page3.

TASK 5: Using putExtra() to pass data to an intent.

In this task, you will use the Intent object to put an Extra that will be read by the second activity. In this way, you can see how to send information to an activity.

Next, you will add the code to each of the .java files to put the Extra to the Intent.

16 | P a g e

Page 17: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

1. Modify the code in MainActivity.java to add an Extra to the Intent. In the following code, you will put the Extra for both of the onClick() handlers.

2. Modify the code in Page2Activity.java to add an extra to the intent.

3. Modify the code in Page3Activity.java to add an extra to the Intent. This time, figure out the code for yourself.

TASK 6: Getting Intent DataIn this task, you will use the Intent object to get the Extra supplied with the previous activity. The following code segment illustrates how to get an Extra. In this case the extra is a string but all data types are supported.

First, note that a <TextView> variable is added to the activity to as to provide a reference.

17 | P a g e

Page 18: ABSTRACT - University of Nevada, Web viewandroid.app.Activity. Even though you have created these activities, they will not work or be compiled into the application. The reason is

The following code gets a reference to the Intent passed to the activity by calling the getIntent() method. i.GetStringExtra(“LastActivity”) gets the key string value of the LastActivity key. The last two statements get a reference to the <TextView> control and store the last activity value into the text property.

1. Activity the file named MainActiviy.java and add the TextView declaration as a class-level variable (TextView textView1)

2. Add the following code to the MainActivity.java file.

3. Add the following code to the Page2Activity file.

4. Add the necessary code to the Page3Activity file.

18 | P a g e