Part 1 - Eclipse and Android Tools
Part 1 - Eclipse and Android Tools
Eclipse
Package explorer - is on the left hand side, you will see the classes that are used within your
project here.
Outline - The outline of a specific class showing what variables and functions it has.
Problems - Tab shows any compilation errors
Console - Shows the eclipse console output - messages from eclipse and android tools (Not the
phone output)
Multiple perspectives - Java, Debug, DDMS, etc.
SDK Manager
Click on eclipse windows -> Android SDK Manager.
Used to setup and update the android sdks. The development stack for android is
The Android SDK and the Dalvik VM have versions associated with them. The latest version is
4.4
Note: Run eclipse as administrator on windows if problems with installing SDK updates.
Android Emulator
Setup an AVD
● Click on eclipse windows -> Android Virtual Device Manager.
● Click New...
● Select a 4” or close to 4” device
● Set the sdk version to 4.4 (or latest version available)
● Uncheck hardware keyboard present, and display skin with hardware keyboard.
● Set sdcard size to 16 mb.
● Click ok.
● Select the newly created emulator in the AVD Manager
● Click start button.
● On the dialog, click launch.
adb logcat
● See the log output of the device
> adb logcat
● Press ctrl+C to stop the log output
● See only the errors in the log
>adb logcat *:E
adb shell
● Connect to the running device
> adb shell
● See the directories on the device
> ls -al
● disconnect from the device
> exit
(The phone operating system is linux based)
adb push
● Create a text file named data (default extension is .txt) in your user directory.
● Pust some values in the data file for checking later - e.g. - 89.9, 44.5
● Move the file to sd card on the emulator device
> adb push C:\Users\anar\data.txt /mnt/sdcard
Note: use forward slashes
● Lookup the file on the device
>adb shell
>cd sdcard
>cat data.txt
DDMS
ddms displays messages from adb within eclipse. It can also be used to take snapshots of the
device or emulator.
logcat
The output from adb logcat shows in the logcat view in DDMS.
The output can also be filtered. Change the output from verbose to error.
snapshot
● Click on the device which you want to take a snapshot
● Click on the camera icon above it.
● DDMS shows a screenshot of the emulator.
● You can save this snapshot for later.
Build
Application is continously built in eclipse as long as Project -> Build Automatically flag is
checked.
Look at the Problems view, and make sure there are no errors
Use clean start a clean build.
Run
Right click on project and click on Run As -> Android Application.
This will launch the application on the already running emulator.
Note: It can take a minute.
Debug
● In the Package Explorer view, double click on src folder to open it.
● Double click edu.uw.first to open that folder.
● Double click MainActivity.java to see the code.
● Activity is the entry point into the android app. 1 page (screen) corresponds to 1 activity.
● onCreate function in activity class is invoked when the app page is displayed.
● Put a break-point in the onCreate function by double clicking on the light blue margin to
it’s left.
● Now launch the application in debug mode.
● Not that it breaks at the break point.
● You can step through functions and look at variable values for debugging the application
● Press F8 to resume the application.
Manifest
● Tells the android VM about the application
● Application is made up of multiple activities
● Name, Icon, Versions supported
● Which activity to launch on start on launch of application.
● Permission requests go in the manifest as well.
Resources
● res folder holds the resources
● images, sound, views, strings, styles.
R.java
● Contains identifiers for resources used in the application.
● Automatically generated from resources defined in the resources files.
● Package name for R for your application is the package name provided in the manifest.
Notes -
OnClickListener is an interface being used as a callback. The callback function, onClick is called
by the button when it is clicked.
We register the callback with the button using the setOnClickListener method.
Functions
public int add(int a, int b) {
return a+b;
}
Parameters in functions are passed by value
A class contains state and behavior. Object state is defined using class properties. Behavior is
methods that execute on the properties. E.g. Put button at class level.
An object is an instance of the class. All classes inherit from the class Object.
Inheritance
Relationship between two classes where the derived class is a type of the base class.
MainActivity inherits from Activity. It inherits the function findViewById from the Activity class.
Hence, it can invoke it to find the button object.
Aggregation
Relationship between two class where the containing class contains (has a) an instance of the
another class. MainActivity can be changed to have a reference to the button at a class level.
Then the MainActivity will contain a button. This button will be available to other functions of the
class too then.
Constructor
Used to create an instance of a class. Has the same name as the class, and no return value
declared. It returns an object of requested type. To create an instance, use new keyword.
Interface
Interface is a set of function declarations. A class implements an interface by providing
implementation of it’s functions. OnClickListener is an interface. We implement it to provide a
callback for the button click.
Inner class
public class MainActivity {
class MyOnClickListener implements OnClickListener() {
public void onClick(View v) {
Packages
A way of grouping related classes together. A way of uniquely naming classes. Classes from
packages other than the one the current class is in have to be imported via the import
statement.
Collections
List - used to hold an ordered collection of objects
Set - Used to hold unordered unique collection of objects
Map - Used to hold key-value pairs for fast lookup
List, Set and Map are interfaces. Commonly used implementations are ArrayList, HashSet or
TreeSet, and HashMap.
Exceptions
Are thrown when an unexpected situation happens.
try {
handleArgs(args);
} catch (Exception e) {
Log.e("First", "Exception occured", e);
}
For e.g. Comment out the line in the code setting the value of button variable.
Now when we run it, it throws a null-pointer exception. This is because the button variable is
null, so, we cannot invoke a function on it. Never catch a NPE.
Note the class and line number from the logcat. For bigger applications, the stack trace can
pinpoint the bug.