Lab Activity

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Lab Activity - Android Activity

• Activity lifecycle
• Switching between activity
• Passing values between activity

Activity Lifecycle
1. Create a new Android project using Java language. Choose empty activity as the template.
2. Import logging library:
import android.util.Log;

3. Write the following inside the Activity class:


String msg = "MyApp : ";

/** Called when the activity is first created. */


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}

/**
* Called when the activity is about to become visible.
*/
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}

/**
* Called when the activity has become visible.
*/
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
}

/**
* Called when another activity is taking focus.
*/
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}

/**
* Called when the activity is no longer visible.
*/
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}

/**
* Called just before the activity is destroyed.
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}

4. Run the App using debug mode.

5. Check the LogCat window and scroll up a bit. You will see the following information:

6. Those are the methods that are called when the app is first starting up. As you can see, the
order of the method called are onCreate() -> onStart() -> onResume().
7. Exercise: Try to unfocus the app by touching/clicking the overview button.
8. Question 1: What are the methods being called? (3 marks)
9. Question 2: What are the methods being called when: (6 marks)
a. Refocus back to the app.
b. Exiting the app.
Switching between activity
1. Create a new android app project using Java language and empty activity. Name the project as
LabActivity2.
2. Design the following interface using constraint layout.

3. You can use my design constraint as a guideline:


4. Now let’s prepare list of values for the Spinner. Open the strings.xml file which placed all String
constant for our app.
5. Add the following String array inside the XML.
<resources>
<string name="app_name">Activity Lab</string>

<string-array name="gender">
<item>Male</item>
<item>Female</item>
</string-array>
</resources>

6. Open the Activity XML file as text so that we can edit the code manually.

7. Add the following to the Spinner declaration:

8. Create new Empty activity inside the project. Name it as RegisterSuccess. Insert a TextView into
the layout with a text “Register Successful”.
9. Modify the Main activity, LabActivity2 java code by adding the following:
package com.example.activitylab; // might be different than yours

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; // import this

public class LabActivity2 extends AppCompatActivity {

private Button regButton; // register button

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_tutorial);

// get the button object by it's id. We declare the button


// object with id = "button" inside our XML file.
regButton = findViewById(R.id.button);
// set onClick() listener for this button
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call a method
openRegisterSuccessActitivity();
}
});
}

private void openRegisterSuccessActitivity() {


// create a new intent for the new activity
Intent intentRegSuccess = new Intent(this, RegisterSuccess.class);
// start the new activity
startActivity(intentRegSuccess);
}
}

10. Test your App by clicking the Register button.


Before click After click

11. You can also change the properties of the widgets inside the same activity. Modify our app so
that the Hello textview below of the form will change its text to “Hello <name> !!” after user
inserted their name. Add the following codes highlighted:
package com.example.activitylab;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LabActivity2 extends AppCompatActivity {

private Button regButton; // register button


private EditText txtName; // name text field

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_tutorial);

// get the button object by it's id. We declare the button


// object with id = "button" inside our XML file.
regButton = findViewById(R.id.button);
// set onClick() listener for this button
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call a method
openRegisterSuccessActitivity();
}
});

// get the EditText for name (refer to XML file for the id)
txtName = findViewById(R.id.editText);
// we use the addTextChangedListener() method to bind to our
// EditText widget. It has 3 abstract methods to be implemented.
// for our activity here, we just use the afterTextChanged() method
txtName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

@Override
public void afterTextChanged(Editable s) {
// get the hello TextView. Refer to the XML file what
// is your id. Mine is textView3. You can change the
// id if you want inside the XML file.
TextView txtHello = findViewById(R.id.textView3);
txtHello.setText("Hello " + s.toString() + " !!!");
}
});
}

private void openRegisterSuccessActitivity() {


// create a new intent for the new activity
Intent intentRegSuccess = new Intent(this, RegisterSuccess.class);
// start the new activity
startActivity(intentRegSuccess);
}
}

12. Test your program.

13. Question 3: Screenshot the app, before and after clicking the button. Insert your name inside
the text field. (6 marks)
Passing values between activities
1. Edit your codes by adding the following:
package com.example.activitylab;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class LabActivity2 extends AppCompatActivity {

private Button regButton; // register button


private EditText txtName; // name text field
private Spinner spGender; // gender spinner

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_tutorial);

// get the button object by it's id. We declare the button


// object with id = "button" inside our XML file.
regButton = findViewById(R.id.button);
// set onClick() listener for this button
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call a method
openRegisterSuccessActitivity();
}
});

// get the EditText for name


txtName = findViewById(R.id.editText);
txtName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

@Override
public void afterTextChanged(Editable s) {
// get the hello TextView. Refer to the XML file what is your
id
TextView txtHello = findViewById(R.id.textView3);
txtHello.setText("Hello " + s.toString() + " !!!");
}
});
}

private void openRegisterSuccessActitivity() {


// get the name
txtName = findViewById(R.id.editText);
String name = txtName.getText().toString();
// get the gender
spGender = findViewById(R.id.spinner);
String gender = spGender.getSelectedItem().toString();

// create a new intent for the new activity


Intent intentRegSuccess = new Intent(this, RegisterSuccess.class);
// set parameters to be sent
intentRegSuccess.putExtra("name", name);
intentRegSuccess.putExtra("gender", gender);
// start the new activity
startActivity(intentRegSuccess);
}
}

2. Next, modify the RegisterSuccess activity Java codes. Add the following:
package com.example.activitylab;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class RegisterSuccess extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_success);

// get parameters inside intent


Intent intent = getIntent();
String name = intent.getStringExtra("name");
String gender = intent.getStringExtra("gender");

// edit the text inside the view. refer to the XML file for the id
TextView txtSuccess = findViewById(R.id.textView4);
txtSuccess.setText("Hello " + name + ". You are " + gender + ".");
}
}

3. Test the app.


First Screen Second Screen
4. If you notice, our second activity doesn’t have a back button. To enable user to go back to the
first activity, we can configure the second activity as the child of the first one. To do so, open the
Android Manifest XML file.

5. Edit the RegisterSuccess activity by adding a parent attribute.

6. Test again your app.


7. Question 4: screenshot the app (first and second screen) by inserting your name in the text
field. (6 marks)
8. Question 5: add another field, age, to the form. Pass age to the RegisterSuccess as integer
data type (you need to parse the String to integer using Integer.parseInt()). Display back the
inserted age in the RegisterSuccess activity. (9 marks)

You might also like