Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project...

75
Android 2: A First Project Kirk Scott 1

Transcript of Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project...

Page 1: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

1

Android 2: A First Project

Kirk Scott

Page 2: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

2

Page 3: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

3

• 2.1 Creating a New, Example Android Application Project in Eclipse

• 2.2 Creating a Virtual Device, an Emulator• 2.3 Running an App on the Emulator in Eclipse• 2.4 Running the Project on an Attached

Device Instead of an Emulator

Page 4: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

4

2.1 Creating a New, Example Android Application Project in Eclipse

Page 5: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

5

• These overheads give a general overview of what is involved in getting a sample application to work—without even looking at its code, how it works, or what Android features it is making use of or depends on

• This is the initial monkey work mentioned in the previous set of overheads

Page 6: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

6

• These overheads are based on the initial example app that is given in the tutorial on the Android developer’s Web site

• If you discover that something in the overheads doesn’t seem to work, it means you can go directly to the tutorial for additional information

Page 7: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

7

A New Android Application

• In the Eclipse menu take the option File, New, Android Application Project

• The result of this is shown in the screenshot on the following overhead

Page 8: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

8

Page 9: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

9

• In the first box, type in the following:• My First App• The other boxes will auto-complete as shown

in the following screenshot• (If the other boxes don’t auto-complete, this is

the first stage where you will have to go fish)

Page 10: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

10

Page 11: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

11

• If things auto-completed as hoped, then you click the Next > button

• I will not show the next screenshots for the wizard• Just keep on accepting the defaults and taking

Next until you reach the Finish option• Notice how this is an area where you’re just doing

monkey work• God knows what all of the options mean at this

point

Page 12: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

12

• Once you click finish “things will happen”• They may happen slowly• I have a reasonably powerful machine and it

takes things a while to happen• There is a progress bar at the bottom which

gives you some indication of what’s going on

Page 13: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

13

• The initial Welcome screen may remain visible• If so, it covers up what has happened• Minimize anything in the way, and with luck,

what you will see in the Eclipse environment will look more or less like the screenshot on the following overhead

Page 14: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

14

Page 15: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

15

• There are several things to notice in this screenshot

• The simplest and most reassuring is that if you can actually read the fine print, you will see that My First App is a Hello World app

• Whenever a new app is created using the defaults, this Hello World layout is provided by default

Page 16: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

16

• Obviously you’re not looking at code• The name of the file that is being shown has an

XML extension• What you’re seeing is the layout of the output

of the app• What you’re seeing is a representation of the

layout as provided in the development environment

• This is not the output showing in the emulator

Page 17: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

17

• You might notice that this screenshot of the editor doesn’t look quite like the one given earlier

• It doesn’t show the tools mentioned in the previous set of overheads, but it’s not exactly the same

• The toolbar happens to have disappeared• It’s not a big cause for concern• You can cause it to appear again or you can access

the tools through the menu

Page 18: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

18

2.2 Creating a Virtual Device, an Emulator

Page 19: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

19

Running the Project on a Virtual Device, an Emulator

• If you want to run the project using the emulator, you need to use the Android Virtual Device Manager to create a new virtual device

• If you take the Android Virtual Device Manager tool (or menu option) you should arrive at something similar to the screenshot shown on the following overhead

Page 20: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

20

Page 21: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

21

• Click the New button to create an Android Virtual Device

• The window shown in the screenshot on the following overhead comes up

Page 22: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

22

Page 23: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

23

• It’s shown on the following overhead with the drop down list for Device expanded

Page 24: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

24

Page 25: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

25

• I tried using various devices• For initial test purposes I finally settled on the

device at the bottom of the list• My theory was that emulation for older,

simpler devices might work better or faster than for newer devices

• On the following overhead the form is shown filled out with representative values for that simple device

Page 26: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

26

Page 27: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

27

Using Snapshot

• Another mystery:• Certain reference sources waffle on the

desirability of the Snapshot option• If you do select Snapshot and then make other

changes, you may have troubles later• Without Snapshot things might run more

slowly, but again, for the first time, try it without Snapshot

Page 28: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

28

Warning

• Here is an example of what can go wrong• I also tried making a virtual device that was a

Nexus tablet (the real hardware I’m working with)

• The memory option defaulted to a RAM value of 1024

• Unfortunately, when I continued with that value of 1024, I got a mystery error

Page 29: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

29

• My Web search informed me that if I changed the RAM value to 512, things should be OK

• For the device I’ve chosen to illustrate with, I took memory size values of 256

• For whatever reason, these values worked, and for the time being, that’s good enough for me

Page 30: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

30

Continuing with Virtual Device Creation

• Once you’ve got the parameters set, click on OK

• The system can drag on for a noticeable amount of time creating the virtual device

• Successful completion is indicated by the appearance of the device in the list as shown in the screenshot on the following overhead

Page 31: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

31

Page 32: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

32

• There is no final OK step after virtual device creation

• You’re done—and you have to close this window

• If you just minimize it, it sits there but is still the active window

• To go on from here, close this window

Page 33: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

33

2.3 Running an App on the Emulator in Eclipse

• There are several different ways of running an app in the emulator

• Later on you will find out that you can create run configurations and debug configurations which are linked to a project

• This is a fine way of doing business, but more complicated than necessary initially

Page 34: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

34

• Starting the emulator can take a lot of time• Trying to run the app and having that process

start the emulator doesn’t seem to be the best plan

• The alternative is to start the emulator and then run the app in it

• This second approach is a better way to do it initially

Page 35: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

35

Starting the Emulator

• The Android Virtual Device screen is shown again on the following overhead with the created virtual device in it

• The screenshot shows the device as being selected and you’ll note a Start… button on the right hand side

Page 36: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

36

Page 37: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

37

• If you click the Start button the following screen should appear

• This is the magic moment• Click Launch

Page 38: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

38

Page 39: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

39

• After clicking launch, you should see the screen with the progress bar shown on the following overhead

• So far so good

Page 40: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

40

Page 41: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

41

• Even before the previous screen goes away, or at the very least, as soon as it finishes, you should see the emulator on the screen

• It’s conceivable that you’ll have to close some things to find it

• It may be hidden• In any case, the initial stage of launch shouldn’t

take a long time, and this is what you should see

Page 42: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

42

Page 43: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

43

Even If Things are Going OK, This is the Painful Part

• What you need now is patience (potentially a lot of it) and faith

• The Android message on the emulated device will flash white for an indeterminate period of time

• The emulator is still launching• As long as that process is going on, there’s

nothing else that you should do• Find something to kill a little time with, because a

watched pot never boils

Page 44: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

44

Edging Towards Success

• Eventually, with good luck, the emulator will look as shown on the following overhead

• In the screenshot that is shown, the emulator screen is grayed out

Page 45: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

45

Page 46: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

46

• If your emulator is grayed out, click and drag over the emulator screen with the mouse

• This should wake it up, as shown on the following overhead

Page 47: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

47

Page 48: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

48

• Click on the circle as directed• If the apps are shown, as they are in the

following screenshot, you have been successful

• The emulator has been launched and is running

Page 49: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

49

Page 50: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

50

Running the Project in the Emulator

• If you want to run the project (without setting up run or debug) configurations, you need a source file for the project open in the Eclipse environment

• The screenshot on the following overhead shows the explorer on the left tracing the path to the MyFirstApp.java file, with that file open in the editor in the middle

Page 51: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

51

Page 52: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

52

• By default, the project is likely to have been built, or will be built when you opt to run

• In the interests of thoroughness, you can also build the project yourself

• In the menu you would take Project, Build All

Page 53: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

53

• To run the app, go to the Run menu in the menu bar and take the Run option in it

• Or, there is a Run tool in the toolbar which you can click on

• There’s no need to take the dropdown option on the tool

Page 54: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

54

• The first time you to try to run it, you may get another dialog box asking what kind of thing you’re trying to run

• You’re given a list of choices, and the choice you want to make is “Android application”

• This dialog is shown on the following overhead

Page 55: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

55

Page 56: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

56

• After choosing the right run type, once again there may be a certain amount of waiting, but with luck, when you look at the emulator, you’ll see what’s shown in the screen shot on the following overhead

Page 57: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

57

Page 58: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

58

• Click and drag over the emulator screen, and it comes to life, as shown on the following overhead

Page 59: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

59

Page 60: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

60

2.4 Running the Project on an Attached Device Instead of an Emulator

Page 61: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

61

• If you haven’t yet run a homemade app on your device, there are essentially 3 steps:

• Getting the Device Ready– You have to get into developer mode so that you it’s

possible to transfer the app to the device• Downloading and Installing the Driver– You need a USB driver in order to transfer through a

USB port• Running the App on the Device– Then you can run the app on the device

Page 62: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

62

• For reference purposes, these overheads were prepared with the following:

• Windows 7• Eclipse, as already described• A Nexus 7 tablet• The fact that it was the Nexus 7 tablet is

relevant to the specific instructions for getting the device ready

Page 63: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

63

Getting the Device Ready

• On the running device, go to the App screen• Tap on Settings• Under System, find About tablet• Tap Build number 7 times• When you return from doing this you should

find Developer options under System

Page 64: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

64

• Take Developer options• Under Debugging, enable USB debugging• You can now connect the device to your

system using a USB cable

Page 65: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

65

Downloading and Installing the Driver

• For information on the Nexus 7 USB driver, if you want any, go to the Android Web site:

• http://developer.android.com• A search on “USB driver” will take you to a

page that has information on the driver• The driver is only necessary when developing

in a Windows environment

Page 66: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

66

• You have a choice on getting the driver• Either you can download from this page• Or you can use the Android SDK Manager

from the ADT bundle in Eclipse• If you’re following this set of overheads and

not using your own development environment, using the SDK Manager is easier

Page 67: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

67

• Using the manager is so easy, there’s not much to say about it

• Click on the tool in the Eclipse environment• Find the line for the driver• Click and follow any instructions

Page 68: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

68

• If for some reason you want to download and install this the hard way, here is a brief list of the steps involved:

• Note which folder the driver is saved in• Right click on the “computer” icon on your

machine• Take the Manage option• Select the Device Manager on the left• Find and expand the Other devices

Page 69: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

69

• Find the Nexus 7 device in the list• You may find the Nexus 7 device somewhere

else depending on your situation• When you find the Nexus 7 device, right click on

it• Take the Update Driver Software… option• Follow the directions and browse to the location

where the driver was saved to• Blah blah blah

Page 70: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

70

Running the App on the Device

• The assumption is that you successfully installed the driver and your device is attached with a USB cable

• Open your app in Eclipse• Take the Run tool or the Run option in the menu• This should cause the Android Device Chooser to

appear on the screen, with the Nexus 7 device in the list

• This is shown on the following overhead

Page 71: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

71

Page 72: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

72

• Select your device and click OK• The magic should happen• The app should run on your device• In addition to running, it will effectively be

installed on the device• If you go to the apps screen you’ll find the

little green Android icon for it and you can run it from there in the future

Page 73: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

73

Summary and Mission

• This is the end of the description of the first project

• As noted, it was only about nuts and bolts• The presentation had nothing to do with code

or the components of the app• Those topics will be pursued in the following

sets of overheads

Page 74: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

74

• You have two missions, neither of which are graded homework:

• 1. Get “Hello World” to work on an emulated virtual device in whatever development environment you’re using

• 2. Get “Hello World” to work on a real attached Android device

Page 75: Android 2: A First Project Kirk Scott 1. 2 2.1 Creating a New, Example Android Application Project in Eclipse 2.2 Creating a Virtual Device, an Emulator.

75

The End