You are on page 1of 10

Midterm Activity 5a

1. Open the Android Studio and create a new Empty Application. Name your application
as Second Activity.
2. In the activity_main.xml, change the value of the TextView from “Hello, World” into
“First Activity”. Below the TextView, add a vertical LinearLayout. Anchor the layout to
the sides of the activity. The activity_main.xml should look like this:

3. Add a button inside the layout. Change the ID attribute btnClick and the text attribute
to Show Second Activity. The activity_main.xml should look like this:

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
4. In the Project Explorer, expand the res folder to show the layout folder. In the layout
folder, one can see the activity_main.xml. This xml file is the layout of the first activity.
To add another activity layout, right-click the layout folder, choose New => Activity =>
Empty Activity.

5. A New Android Activity dialog box will appear. Change the Activity Name to
SecondActivity. Take note that the Layout Name will automatically change based on the
Activity Name. Make sure that the Generate Layout File and the Backwards
Compatibility are selected. Click Finish to generate the files.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
6. A new java and xml files can now be seen in the Project Explorer. Open the
activity_second.xml in the editor.

7. Add a TextView, vertical LinearLayout and a Button in the second activity. The
activity_second.xml should look like this:

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
8. Open the MainActivity.java in the editor. This is the file where we will put the code
that will open the second activity from the main. Use the AutoComplete feature of the
editor by typing the first few characters, select from the list of suggestions, and press
the Tab key.

This will also ensure that the necessary package will be automatically included. Again,
USE THE AUOTCOMPLETION!
9. Inside the OnCreate method, create a button object that will be used to link with the
button resource. Use the findViewById to link the button resource to button object.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
10. Set the event handler of the button to respond to the button’s click. Remember to
use the autocomplete feature when typing the code.

NOTE:
setOnClickListener - used to execute a part of code when a button or other part of the
user interface is touched (clicked).
View.OnClickListener - Interface definition for a callback to be invoked when a view is
clicked.
11. To open the second activity from the first, we will use intent. Intent is a description of
an operation to be performed. Inside the onClick method of the button event handler,
put the intent to open the second activity.

This will tell the Android application to open the second activity when the button is
pressed.
12. Before running the program, we need to register the second activity to the
application manifest. In the Project Explorer, open the app => manifests =>
AndroidManifest.xml. Change the activity tag of the SecondActivity to this:

The manifest file describes essential information about your app to the Android build
tools, the Android operating system, and Google Play. By adding the
android:parentActivityName attribute, the activity will have a up navigation button that
will allow the user to return to the main activity.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
13. Run the application. Output should look like this: Take note that when the second activity is
displayed, an up navigation will appear.

ACTIVITY:
Add an event to the button of the second activity. When the button of second
activity is clicked, it should open the first activity.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
Midterm Activity 5b
1. In this activity, we will create an application that uses explicit intent. Still using the
Second Activity project, add a vertical LinearLayout on top of the TextView of the
activity_main.xml. Make sure that the layout is anchored to the activity. Add an EditText
(Plain Text) in the layout. Change the value of the ID attribute to etData and make the
value of text attribute to empty. activity_main.xml should look like this:

2. Open the activity_second.xml. Add a vertical LinearLayout on top of the TextView.


Add a TextView to the layout. Enlarge the size of text of the TextView to 30sp. Change
the ID to tvData and remove the value from the text attribute. activity_second.xml
should look like this:

3. In the MainActivity.java, we will use the intent to pass data from one activity to
another. Create an EditText object that will be used to link with the plain text resource.
Use the findViewById to link the plain text resource to EditText object. Reminder, use
the autocomplete feature of the editor.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
4. In the onClick event handler method of a button add the following code:

Here, we will use the String object to store the data that was typed in the edit text.
NOTE:
getText().toString() method of the edit text object is used to convert values from a
control into string and store the value to a variable.
putExtra() is used to add extended data to the intent. The first argument is the
identifier. Identifier should not contain a space. The seconde argument is the data we
want to pass to another activity.
5. In the SecondActivity.java, we will get the data that was passed from another activity.
Create a TextView object that will be used to link with the text view resource. Use the
findViewById to link the text view resource to TextView object.

6. Type the following inside the onCreate method:

NOTE:
getIntent() - Return the intent that started this activity.
getStringExtra() - Retrieve extended data from the intent.
setText() - Sets the text to be displayed.
7. Run the application. Enter a value to the edit text and when the button is pressed, the
value you entered in the first activity should be displayed in the text view of the second
activity.

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
ACTIVITY:
Add an edit text on the layout above the text view showing “Second Activity”.
Add another button below the “BACK TO FIRST ACTIVITY”. Text of the new
button should be “Submit”. When the “SUBMIT” button is pressed, the value
entered on the edit text should appear on the third activity. Third activity should
display the data entered in the second activity and should contain an up
navigation that goes back to the main activity. Output should look like this:

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1
First Activity Second Activity Third Activity

COMP-602 JOSEPH ROSS E. CORTEL


Mobile Application Development Instructor 1

You might also like