Вы находитесь на странице: 1из 4

BITP 3453 – Lab 2

( Intent, New Activity and Multithreading)


 Native Android would specify many activities in an application where each activity
will bind to a xml file to define the GUI. This XML file is attached or bind to R.java
class
 Intent is a class that usually used to start another activity and carry information
from one activity to another activity. It is also used to interact between different
application in a device.
 Native Java Android support thread which is similar to any Java application which
use Runnable and Thread class

1) Creating a new Project in Android Studio


- Select File >> New >> New Project >> Give a proper applicaiton name such as
BITP3453Labs
- Select any minimum SDK required(usually just let the default unless you really want
to target to certain Android version prior to using latest class and method)
- Select Empty Activity when Adding an Activity.
- Name it FirstActivity and click finish
- Once Android Studio has finished loading and building up Gradle open the layout
activity_first.xml
- Add the two Text fields, one Button and one Large Text as shown in Figure 2.1
below. Give a proper id for the components such as edtTxtName, edtYear,btnMe
and txtvwAge.

Figure 2.1

- To add logical process, open the FirstActivity.java and add the following code:

1
- For the button, you can either declare a void method as follow public void
fnGreet(View vw) :

2) Creating a new activity and passing via Intent


- Right click on your app project >> New >> Activity >> Empty Activity >> Activity
Name : ThreadedActivity >> Finish
- By right two files will be added one is ThreadedActivity.java and
activity_threaded_main.xml
- Open back the previous main activity and add a button. Change the button text to:
To New Activity
- Double click the button and add android:onClick = “fnThreadActivity”
- Clean and Build your project
- Open the HelloMainActivity and add new void function called. Define the function
base on code snippet below :

This variable will be


carried to the next activity

- Now open the ThreadedActivity.java and find method onCreate(). This method
means upon starting the activity, this method will be executed first. Add the
following code snippet:

2
- Open the activity_threaded_main.xml, (See Figure 2.2 ) add:
1. ImageView (change ID : imgVwProfile)
2. Button (Change ID : btnTakePic, Text: Take Picture!)
3.

Figure 2.2
- For the new button, add onClick: fnTakePic

3) Defining fnTakePic with Thread


- Add the following code snippet below

3
- The code above will execute another Android activity named
ACTION_IMAGE_CAPTURE
- Note that the ACTION_IMAGE_CAPTURE (line 58 -59) activity execute on different
thread
- runOnUiThead(line 61 – 73) is another thread separated from the
ACTION_IMAGE_CAPTURE thread
- startActivityForResult() at line 59 is a method that will trigger another Intent
activity which has similar function as method startActivity()
- The different is that startActivityForResult() will contain data/information from
another activity where you can manipulate by defining onActivityResult()
- Add the following code snippet to onActivityResult() :

Assignment :
1) On the FirstActivity, find the age of the user based on the year born. Display the
age by appending the textview. Sample output: “Helloo and welcome Ramli.
You are 40 years old”.(currentyear from android studio)
2) Create an ImageView at the first activity and pass image that is captured back
to the first activity previously(image dari threaded to firstactivity)

Вам также может понравиться