72
Android 3: Exploring Apps and the Development Environment Kirk Scott 1

Android 3: Exploring Apps and the Development Environment Kirk Scott 1

Embed Size (px)

Citation preview

Page 1: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

1

Android 3: Exploring Apps and the Development Environment

Kirk Scott

Page 2: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

2

Introduction

• This unit surveys some of the code aspects of an Android app

• It also surveys how you can find these aspects of an app through the Eclipse interface

• The unit is not a comprehensive treatment of these topics

Page 3: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

3

• The unit is intended to answer the questions that might come to the mind of a programmer who is seeing app development for the first time

• It includes this simple example as a way of getting familiar with apps:

• How do you change MyFirstApp, the “Hello World” app so that it will display a different message?

Page 4: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

4

• The outline for this unit is given on the following overhead

• The outline corresponds to parts of the project as they can be found in the Project Explorer in Eclipse

• At first sight the outline will probably not mean much

• The point is that various parts of the app will be found through the explorer, shown, and briefly explained

Page 5: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

5

Outline

• 3.1 The Project Explorer• 3.2 /src/com.example.myfirstapp/

Main_Activity.java• 3.3 /res/layout/activity_main.xml• 3.4 /res/values/strings.xml• 3.5 /gen/com.example.myfirstapp/R.java• 3.6 Other Things in the Project Explorer• 3.7 Changing the App

Page 6: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

6

3.1 The Project Explorer

Page 7: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

7

• The screenshot on the following overhead shows the hello world app as it was shown in the previous overheads

• The point of interest at this moment is the Project Explorer

• It is on the left hand side of the screen, and in this screen shot, various folders have been expanded

Page 8: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

8

Page 9: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

9

• You will always want the explorer open in order to navigate through the parts of a project

• If, by chance, the explorer has disappeared from view in your copy of Eclipse, you can always restore it

• Take these options in the menu:• Window, Show View, Project Explorer

Page 10: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

10

3.2 /src/com.example.myfirstapp/Main_Activity.java

Page 11: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

11

• In the screen shot on the following overhead, the MyFirstApp folder has been expanded to show the items underneath it

• In it you will find the MainActivity.java source file for the app

• By double clicking on it, you can open it in the editor

• This is what is showing in the editor in the screen shot

Page 12: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

12

Page 13: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

13

• This is essentially the Java source code for the app

• Notice that unlike a Java application, there is no main() method

• The app code is slightly reminiscent of applet code, if you are familiar with that

Page 14: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

14

• The app class extends the Activity class• Instead of a main() method it has (overrides)

an onCreate() method• Most of the details mean nothing at this point• Note that in the code, reference is made to a

class name R• This is one of the parts that will be shown

shortly

Page 15: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

15

3.3 /res/layout/activity_main.xml

Page 16: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

16

• In some programming languages, the layout, or visual appearance of an application is defined within its source code

• A significant feature of Android development is that the layout is defined separately from the code

• We saw the Java code for MyFirstApp in MainActivity.java in the previous section

Page 17: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

17

• The screenshot on the following overhead shows the layout of the app

• You open this when you double click on activity_main.xml under /res/layout in the Project Explorer

• This is the view of the layout that you get when the Graphical Layout tab has been selected at the bottom of the editor

Page 18: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

18

Page 19: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

19

• The screen shot on the following overhead shows the actual source code for the layout

• You see this when the activity_main.xml tab has been selected at the bottom of the editor screen

• The layout is defined in XML—not in Java code

Page 20: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

20

Page 21: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

21

• On the following overhead the graphical layout is shown again

• When you are in this view, this is what you also see:

• Between the explorer and the layout is a palette of graphical tools and components for creating visual layouts for apps

• Developing in Eclipse involves becoming familiar with these graphical tools

Page 22: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

22

Page 23: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

23

• On the following overhead the source file is shown again

• The layout file includes layout syntax• The syntax isn’t of particular interest at the

moment• However, it’s worth noting that some of the

keywords are descriptive enough to give some idea of what’s going on

Page 24: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

24

Page 25: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

25

• In addition to giving information about the project explorer, the example is illustrating various things about Android apps

• To repeat:• The Java code for the app is saved in one file• The layout is defined in XML and saved in

another file• You need to be able to find each, so you can

work with them

Page 26: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

26

• Consider the XML source code for the layout of the app one more time

• Look in the following screen shot and you will not find the string “Hello World!” which the app produces as output

• Strictly speaking, “Hello World!” is the output of the app, but it is not part of the layout

Page 27: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

27

Page 28: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

28

• What represents the string “Hello World!” in the layout?

• It is this:• android:text="@string/hello_world"

Page 29: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

29

• This refers to a resource belonging to the app which is defined elsewhere in the environment

• Not only are the Java and XML files saved separately in an Android app

• The resources are also saved separately

Page 30: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

30

3.4 /res/values/strings.xml

Page 31: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

31

• The screenshot on the following overhead shows the Project Explorer open to show /res/values/strings.xml

• The editor shows what you see when you double click on strings.xml

• (Note that of the two tabs at the bottom of the editor screen, you need to be on strings.xml, not Resources, if you want to see the XML source code)

Page 32: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

32

Page 33: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

33

• This is where you find the string “Hello World!”

• This string is a resource of the app• The Java source code or XML layout code for

an app displays a resource by reference• As noted earlier, in the layout file, this is how

“Hello World!” is referred to:• android:text="@string/hello_world"

Page 34: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

34

• The resource, a string, is defined in strings.xml, separate from the Java and XML code

• This separation of resources from code is a significant element of Android development

• It will be explained in greater detail in following sets of overheads

Page 35: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

35

3.5 /gen/com.example.myfirstapp/R.java

Page 36: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

36

• The screenshot on the following overhead shows the project explorer open to the file R.java in the editor

• R.java is an auto-generated file the results when an app is successfully compiled

• R.java is kind of a global container associated with the app

Page 37: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

37

Page 38: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

38

• The onCreate() method in MainActivity.java contained the following line of code:

• setContentView(R.layout.activity_main);

Page 39: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

39

• It’s too soon for an elaborate explanation• However, this much is already apparent:• The Java source code refers to the XML layout

for the app through the R.java file• Different aspects of an app are stored in

different files• R.java establishes some names by which XML

components are accessible in Java code

Page 40: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

40

• A closer look shows that R.java contains final declarations

• These declarations represent the constants associated with the app

• It’s also apparent that hexadecimal values are being used

• A discussion of how to use R.java when developing an app will come later

Page 41: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

41

3.6 Other Things in the Project Explorer

Page 42: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

42

• There are many other things that can be found through the project explorer

• We will not consider the majority of them• A few more will be mentioned briefly• It is not necessary to understand these things

as much more than vocabulary at the moment

Page 43: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

43

/gen/com.exmple.myfirstapp/BuildConfig.java

• If you double click on BuildConfig.java in the Project Explorer you see what’s shown on the following overhead

• This is an auto-generated file• It is stored along side R.java

Page 44: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

44

Page 45: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

45

• Building is roughly synonymous with compiling• An app that has been brought to the point of

being runnable will have a build configuration• It is possible to create a build configuration

step-by-step• It is also possible to let the build configuration

come into existence by default• That’s what we’ll do for the time being

Page 46: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

46

Android 4.x and Android Private Libraries

• The screenshot on the following overhead shows a subset of what you see when you double click on Android 4.x in the Project Explorer, on the left

• (The file R.java is still showing in the editor)• The Android 4.x folder is a library which contains

Android packages and if you were to scroll down further, you would find Java packages

• The Android Private Libraries folder is similar• In effect, what you’re seeing is the set of API packages

available when creating Android apps

Page 47: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

47

Page 48: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

48

/bin/res/MyFirstApp.apk

• This section starts with some information taken from Wikipedia, starting on the following overhead

Page 49: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

49

APK (file format)From Wikipedia, the free encyclopedia

• Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google's Android operating system. To make an APK file, a program for Android is first compiled, and then all of its parts are packaged into one file. This holds all of that program's code (such as .dex files), resources, assets, certificates, and manifest file. As is the case with many file formats, APK files can have any name needed, but must end with the four character, three letter extension, .apk.[1][2][3][4]

Page 50: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

50

• APK files are ZIP file formatted packages based on the JAR file format, with .apk file extensions. The MIME type associated with APK files is application/vnd.android.package-archive.[5]

Page 51: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

51

• In short, the apk file is a jar file for an Android app, which packages up the manifest and the compiled source code for distribution

• The screenshot on the following overhead shows what you see when you double click on MyFirstApp.apk in the Project Explorer

• Not surprisingly, it looks more or less like what you see when you open up a class file in an editor—binary nonsense

Page 52: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

52

Page 53: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

53

/bin/res/AndroidManifest.xml

• The screenshot on the following overhead shows what you see when you double click on AndroidManifest.xml in the Project Explorer

• There is something in Java called a jar file• Java jar files have manifest files• An Android apk file, the result of building a

project, is effectively a kind of jar file• Every completed app, every apk file, will have a

manifest file associated with it

Page 54: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

54

Page 55: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

55

/res/menu/main.xml

• The screenshot on the following overhead shows what you see when you double click on main.xml under /res/menu in the Project Explorer

• There is nothing of consequence here for the moment

Page 56: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

56

Page 57: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

57

• The layout that you became familiar with above is /res/layout/activity_main.xml, not what you see here

• However, menus will be a topic of interest in Android development later on

Page 58: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

58

3.7 Changing the App

Page 59: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

59

What about strings.xml?

• strings.xml was covered earlier in this unit• Practically speaking, it’s the most significant

item covered so far• Having taken the tour of the explorer and

encountered strings.xml, we are in the position where we can make a simple, initial modification to the app

Page 60: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

60

• The following overhead shows a screenshot of strings.xml where the string has been change from “Hello World!” to “Good-Bye Cruel World!”

Page 61: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

61

Page 62: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

62

• At this point, if you’ve made changes to strings.xml, you can go back to the MainActivity.java, as shown on the following screenshot

• A change in the output string of the app requires absolutely no change in the Java source code

Page 63: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

63

Page 64: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

64

• From MainActivity.java, you can run your application, whether on the emulator or on an attached device

• When you click run, you’ll be prompted to save the changes to strings.xml if you didn’t save out of that editor screen

• Ta-Da: The following screenshot shows success on the emulator

Page 65: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

65

Page 66: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

66

Summary and Mission

• This is the end of the initial presentation of the components of an app that can be found in the Project Explorer

• High points of what can be found there:• MainActivity.java, the source code• activity_main.xml, the layout• strings.xml, the file containing the string resources for

an app• R.java, the file containing resources as defined in the

Java code for the app

Page 67: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

67

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

• Mission 1• Create a new Android project and modify it so

that its output is not “Hello World”• This should work, and should consist

essentially of changing strings.xml

Page 68: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

68

• The point of this mission is obviously not the importance of the change

• The point is finding strings.xml and reinforcing what relationship it has with the app code

• Another point is just getting in practice running apps, whether on the emulator or on an attached device

Page 69: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

69

• Mission 2• Things related to this were mentioned only in

passing in this set of overheads, but it’s not too soon for you to conduct a small experiment in preparation for coming attractions

• Using the palette of graphical tools for activity_main.xml, drag and drop some new item into the layout for an app

Page 70: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

70

• Note the contents of R.java before building the project

• Then try building the project and consider two things:

• A. Do you get error messages?• If so, what are they, and what do they imply?• B. Were there any changes in R.java after the

changed version was built?

Page 71: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

71

• The point of the second mission is not necessarily for you to have a firm grasp of what’s going on

• The point is just to have you find activity_main.xml, fiddle with the graphical tools palette, and then find R.java in the explorer

Page 72: Android 3: Exploring Apps and the Development Environment Kirk Scott 1

72

The End