0% found this document useful (0 votes)
67 views

Part 1 - Eclipse and Android Tools

This document provides an overview of the Eclipse integrated development environment (IDE) and tools for Android development. It covers the Eclipse package explorer, outline view, problems view, console view, SDK manager, Android virtual devices, Android debug bridge (ADB), Dalvik debug monitor service (DDMS), and creating a basic "Hello World" Android project with activities, resources, and buttons. It also includes questions and answers about Java concepts like classes, objects, inheritance, and interfaces.

Uploaded by

imran hameer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Part 1 - Eclipse and Android Tools

This document provides an overview of the Eclipse integrated development environment (IDE) and tools for Android development. It covers the Eclipse package explorer, outline view, problems view, console view, SDK manager, Android virtual devices, Android debug bridge (ADB), Dalvik debug monitor service (DDMS), and creating a basic "Hello World" Android project with activities, resources, and buttons. It also includes questions and answers about Java concepts like classes, objects, inheritance, and interfaces.

Uploaded by

imran hameer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

Install Android 4.4 SDK samples


● Check android 4.4 -> Samples for SDK
● Click install packages...
● Click Accept on the next dialog
● Go to where you installed ADT bundle in windows explorer
● Click on sdk directory.
● Now it should have a Samples directory.
● This is sample code on how to use the Android SDK.

SDK samples are a great resource for learning.

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.

It will take a few minutes to launch the emulator

Android Debug Bridge (ADB)


ADB is used to log and debug the software on the device.
ADB is a command line tool.
adb help
● Go to the location where you installed the adb bundle.
● Change the directory to adt-bundle-<your platform>
> cd adt-bundle-windows
● Go to the sdk directory
> cd sdk
● Go to platform tools directory
> cd platform-tools
● See what options are available in adb
>adb help
adb devices
● See what devices are connected
> adb devices
● It should show the emulator that you launched previously.

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.

DDMS Reference - http://developer.android.com/tools/debugging/ddms.html

Part 2 - Lab - First Android Project


Create the project
● Get back to the java view
● Click on File -> New -> Android Application Project
● Application Name = First
● Project Name = First
● Package name = edu.uw.first
● Min Required SDK = 2.3
● Target SDK = 4.4
● Compiling with SDK = 4.4
● Read the next screens to see what options are available.
● Select default for all the remaining screens.
● Click finish on the last screen

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.

Part 3 - Parts of an Android Project


Switch back to the Java Perspective
Activity
● Derived from Activity
● OnCreate method is called when the activity is launched
● setContentView method call sets the view using the definitions in the layout xml
● The view definition is in res/layout/activity_main.xml

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.

Part 4 - Lab - Text


Change text from ‘Hello World’ to ‘Welcome to UW’
● Go to the main activity - MainActivity.java
● Find the layout that the activity is using - activity_main
● Go to the layout file in res/layout - activity_main.xml
● In the graphical layout, see the properties view on the right.
● Change the text value for the text view to Welcome to UW.
● Re-run the application in the emulator to see the change.

Print text on Log


● In MainActivity, onCreate function, at the end type in Log.v("First", "Welcome to UW");
● Log is a class provided with android. It has functions to write to android log
● The function v writes the log as verbose output.
● The first argument to the function is a seachable tag
● Second argument is the message.
● Use cntrl+shift+O to import the Log class
● Create a filter in logcat for the tag ‘first’. Give it a name.
● Save and re-run the application
● When the activity is loaded, you should see the log message show in logcat.

Part 5 - Lab - Button


Add a button using the design view for the layout
Go to the layout file in res/layout - activity_main.xml
Drag and drop a button to the view. Position it where you see fit.
Note that if open the xml tab, it shows a textview and a button in the xml.
Save and re-run the application.
You should see the button on it.
When you click the button, nothing happens.

Log when the button clicked


For Main Activity, implement OnClickListener
public class MainActivity extends Activity implements OnClickListener {
...
}
Add the following function to the MainActivity class
@Override
public void onClick(View v) {
Log.v("First", "Lesson 1");
}

In Main Activity, inside the onCreate function, type in


Button button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(this);
Save and re-run the application.
When you click the button, you should see Lesson 1 printed in the filtered log.

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.

Part 6 - Java Q&A


Primitive Variable types
● boolean
● char
● byte
● short
● int
● long
● float
● double

Reference variables - String, Object, Anything derived from Object

Functions
public int add(int a, int b) {
return a+b;
}
Parameters in functions are passed by value

Classes. Create. Inherit. Interface

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.

An object can reference other objects.

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) {

Log.v("First", "Lesson 1");


}
}
protected void onCreate(Bundle savedInstanceState) {
...
MyOnClickListener listener =
new MyOnClickListener();
button.setOnClickListener(listener);
}
...
}

Anonymous Inner Class


button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("First", "Lesson 1");
}
});

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.

You might also like