Upload
eric-percival-wilcox
View
214
Download
1
Tags:
Embed Size (px)
Citation preview
1
Android 3: Exploring Apps and the Development Environment
Kirk Scott
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
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?
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
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
6
3.1 The Project Explorer
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
8
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
10
3.2 /src/com.example.myfirstapp/Main_Activity.java
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
12
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
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
15
3.3 /res/layout/activity_main.xml
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
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
18
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
20
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
22
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
24
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
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
27
28
• What represents the string “Hello World!” in the layout?
• It is this:• android:text="@string/hello_world"
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
30
3.4 /res/values/strings.xml
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)
32
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"
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
35
3.5 /gen/com.example.myfirstapp/R.java
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
37
38
• The onCreate() method in MainActivity.java contained the following line of code:
• setContentView(R.layout.activity_main);
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
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
41
3.6 Other Things in the Project Explorer
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
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
44
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
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
47
48
/bin/res/MyFirstApp.apk
• This section starts with some information taken from Wikipedia, starting on the following overhead
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]
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]
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
52
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
54
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
56
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
58
3.7 Changing the App
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
60
• The following overhead shows a screenshot of strings.xml where the string has been change from “Hello World!” to “Good-Bye Cruel World!”
61
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
63
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
65
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
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
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
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
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?
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
72
The End