Professional Documents
Culture Documents
COMP-602 Joseph Ross E. Cortel Mobile Application Development Instructor 1
COMP-602 Joseph Ross E. Cortel Mobile Application Development Instructor 1
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:
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.
7. Add a TextView, vertical LinearLayout and a Button in the second activity. The
activity_second.xml should look like this:
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.
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.
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.
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.
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.
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.