Hardware Reference
In-Depth Information
working with Android code; three are automatically generated during project generation:
main.xml
,
strings.xml
, and
AndroidManifest.xml
; the fourth,
accessory_filter.xml
, is not. The following list describes these four files:
main.xml
contains the layout for an application's first screen. The file contains descriptors
for onscreen object layout, type, and identification. Eclipse has a built-in graphical tool for
choosing and placing objects, and gives you the ability to view the XML. The
main.xml
file is
located in the workspace of the project, in the
res/layout/main.xml
folder.
•
•
strings.xml
is used to define static information in the form of character strings. This file
can be used to define information from many different attributes, such as object names,
information for text fields, and file locations. This file is not absolutely necessary for small
applications being developed, but note that Eclipse will throw a warning when strings are
hard coded. Using
strings.xml
is good programming practice and is useful for when multiple
instances of data are used throughout an application, providing a single place to update
instead of requiring that every occurrence in the code be changed. The
strings.xml
file is
located in the workspace of the project, in the
res/values/strings.xml
folder.
•
AndroidManifest.xml
defines what package and activity need to start when the application
is launched, the type of the application, what system events are responded to, and some
miscellaneous information for the Android device, along with the icons to be used.
AndroidManifest.xml
is similar to a
main
function in C or C++; this is where the application
enters and starts. Android uses intent filters that are checked when a system event occurs,
and if the filter in the file matches the event, the application can be started (e.g., when a call
is received, an application is run for the user). The
AndroidManifest.xml
file is located in the
workspace root.
•
The
accessory_filter.xml
file needs to be created by the programmer and is used when
AndroidManifest.xml
responds when accessories are attached. The data in this file is
compared to the created data in the Arduino sketch when a connection is getting established.
To create this file, you need to create a folder in the workspace's
res
folder named
xml
and add
a new file to this folder, named
accessory_filter.xml
.
■
Before you insert the code from the listings in this chapter, you need to delete the
styles.xml
file and the
menus
folder from the
res
folder and the project's workspace.
Note
The application framework and workspace were already created for this example when the Hello World
application was created to test the functionality of the development environment. Step through the code in the
examples following and compare to the code generated to get a feel for how everything is structured, and then
replace the code in the workspace project with the appropriate example code.
■
eclipse with the android SdK is sometimes a bit temperamental; errors that report that
R.*
is not capable of
resolving are caused by the
R.java
not being generated. try a clean and rebuild the project after checking for errors in
the XML files. try ctrl + shift + o to organize imports and select the activity if clean and rebuild doesn't work.
Note