1
Islamic University of Gaza Eng. Jehad Aldahdooh
Faculty of Engineering Mobile Computing
Computer Engineering Dept. Android Lab
To create a new Android Project follows the steps:
1. Open Eclipse.
2. Click the menu File New Android Application Project
Lab 1: Getting Started With Android Programming
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
2
3. Name the project : In this stage, as shown below there are exist three names described as:
Application Name
Shown in Play Store and Settings .Usually same as Project Name.
Project Name
Eclipse project name. Follow naming convention you use for Eclipse. Not used
elsewhere.
Package name
Apps on a particular Android device must have unique packages, so use
com.yourCompany.project
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
3
Target Build SDK
The Android version used to build/compile your project. This can be any version,
but the safest option is to make it match the minimum SDK below.
Minimum Required SDK
The Android version that you want to run on.
4. Click Next
5. Configure Launcher Icon
Purpose
To choose the picture displayed on the Android device, that, when clicked,
launches the app.
Defaults
Use defaults for development and testing. Just press “Next”.
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
4
6. Choose “Blank Activity”
Options for Blank Activity:
Activity Name
Name of “main” Java class. This is the class you will edit first. Class name often
corresponds to project name.
Layout Name
Base name of XML file in res/layout folder. Used to give layout to app. often
just called “main”. Will be referred to in main Java class with
R.layout.layout_name.
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
5
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
6
Anatomy of an Android Application
Source Code
Java files auto-
generated by ADT Version of the
library you had
chosen
Anything stored in the
'assets' folder has to
be accessed in the
classic 'file'
manipulation style
Resources that
your application
may need
Value type
resources,
such as string
and integer
constants
Use to construct
user interfaces
Hold image and
animation files that
you can use in your
application.
Contains all the
information about the
application
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
7
First: On the Emulator
Before we can run the application, we need to setup an Android Vitual Device(AVD), or
emulator, to run it on:
Select the menu Window -> "Android SDK and AVD Manager".
Select Virtual Devices on the left hand side.
Click the New... button.
Give your AVD a name.
Select the target build that we would like to run the application on
Click Create AVD and close out the SDK/AVD Manager.
We're now ready to run our application.
Select the menu Run -> Run.
Note: The emulator may take a long time to start up.
Note: Another way to run your application is to right-click on the project in the
Package Explorer, then select Run As -> Android Application.
Run “Hello World” on the Emulator and Phone
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
8
Second: On a Physical Device
Project Modifications
From the Package Explorer, double-click the file AndroidManifest.xml.
Select the tab labeled AndroidManifest.xml along the bottom.
Add android:debuggable="true" to the inside of the opening <application> tag.
Save the file and close it.
Phone Modifications
o Select Settings -> Applications -> Development.
o Enable the USB debugging option
Ensure the device is properly connected. Run the application as you would normally.
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
9
1. Java-based
Use Java to define Strings, lay out window, create GUI controls, and assign event
handlers. Like Swing programming.
1. XML-based
Use XML files to define Strings, lay out window, create GUI controls, and assign
event handlers. The Java method will read the layout from XML file and pass it to
setContentView.
2. Hybrid
Use an XML file to define Strings, layout window and create GUI controls. Use Java
to assign event handlers.
1. Java-Based Approach: Template Approach
Use Java to define Strings, layout window, create GUI controls, and assign
event handlers.
Advantages
Familiar to Java desktop developers. Like approach used for Swing, SWT, and
AWT.
Good for layouts that are dynamic (i.e., that change based on program logic).
Disadvantages
Harder to maintain.
Not generally recommended except for dynamic layouts.
But still acceptable for App Store. Whatever works best for your programmers
and your app. No code police.
Three Main Approaches in Android Programming
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
10
OnClickListener is a public inner class inside View. But, as long as you import
android.view.View.OnClickListener, you use it just like a normal class. And, remember that
Eclipse helps you with imports: just type in the class name.
2. XML-Based Approach: Template Approach
Use XML files to define Strings, layout window, create GUI controls, and
assign event handlers.
Define layout and controls in res/layout/main.xml
Define Strings in res/values/strings.xml
Advantages
Easier to maintain
Can use visual layout editor in Eclipse
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
11
Disadvantages
Works poorly for dynamic layouts
3. Hybrid Approach: Template
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
12
Activity: Much like a Form for a web page, activities display a user interface for the
purpose of performing a single task. An example of an Activity class would be one which
displays a Login Screen to the user.
Create the Activity Class
create a new class that extends android.app.Activity class and implements
android.view.View.OnClickListener interface.
Add a method with the following signature: public void onCreate(Bundle
savedInstanceState) .
o This method will be called when the Activity starts and is where initialization of local
and member data will be done.
Inside this method perform the following:
Activity Class
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
13
o make a call to super.onCreate(savedInstanceState)
This should always be done and is to ensure that any necessary parent class
initializations are performed.
o make a call to this.setContentView(R.layout.activity_main)
When you created the XML layout file earlier, the Android Eclipse Plugin
automatically added a static constant to the static R.layout class in the R.java
file under the /gen folder. This constant variable has the same name of the file
and it's value is used to identify the layout file.
This call tells Android to create a screen based off of the layout file.
o Make a call to this.findViewById(R.id.button) and set your Button member variable
equal to the return value.
Implement the OnClickListener interface by creating a method stub with the following
signature: public void onClick(View v) .
The Activity base class defines a series of events that govern the life cycle of an
activity. The Activity class defines the following events:
onCreate() — Called when the activity is first created
onStart() — Called when the activity becomes visible to the user
onResume() — Called when the activity starts interacting with the user
onPause() — Called when the current activity is being paused and the previous
activity is
being resumed
onStop() — Called when the activity is no longer visible to the user
onDestroy() — Called before the activity is destroyed by the system (either
manually or by the system to conserve memory
onRestart() — Called when the activity has been stopped and is restarting again
Android Lifecycle
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
14
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
15
L a b 1 : G e t t i n g S t ar t e d W i th A n d ro i d P r o g r a m mi n g M o b i l e Co m pu t i ng - A n d r oi d
L a b
16
1. Run the application .
2. When the activity is first loaded, you should see something very similar to the following in
the LogCat window
In the onCreate() event
In the onStart() event
In the onResume() event
3. If you click the Back button on the Android emulator, the following is printed:
In the onPause() event
In the onStop() event
In the onDestroy() event
4. Click the Home button and hold it there. Click the Activities icon and observe the following:
In the onCreate() event
In the onStart() event
In the onResume() event
5. Click the Phone button on the Android emulator so that the activity is pushed to the
background.Observe the output in the LogCat window:
In the onPause() event
In the onStop() event
6. Notice that the onDestroy() event is not called, indicating that the activity is still in memory.
Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the
output in the LogCat window:
In the onRestart() event
In the onStart() event
In the onResume() event