200 likes | 273 Views
Introduction to Android (Part 4) Folders, XML, Editors, Logs. Folders.
E N D
Folders An Android project is composed of a number of parts.Folders are used to organize most of the files created by the developer and created by the Android tools. The Package Explorer View (on right) presents a logical view of the folders that is close to the physical arrangement.1
Folders: src The src folder contains Java source code. When an Android project is initially created, a single Activity class is created. Java packages are seen as subfolders inside the src folder. Certain source files in an Android application must be contained in a two level package hierarchy. In this example, the package is cs2302.firstproject.
Folders: gen The gen folder contains specially generated files that are used and accessed by your application, such as the ‘R.java’ file that contains Java source representations of identifiers in the various resource files. You should never edit the files in the gen folder.
Folders: res The res folder contains resources used by the application. These resources are files in a variety of formats including bitmap images such as JPEG and PNG. One common format is text files in XML. There are several subfolders of the res folder.
Folders: drawable The various ‘drawable’ folders contain images in various formats. The different folders are intended to contain parallel sets of images suitable for different device resolutions. A folder with the name drawable alone, though not provided by the IDE, will hold resources to be used on any platform.
Folders: layout The layout folder contains files that describe the various views used by an application. When a new project is created using the ADT, the folder contains a single xml view file named main.xml, or activity_…xml if you change your default activity name. This view is used by the initial application. If an application only has one view, it would be sufficient to edit this default view.
Folders: values The values folder contains files that define named constants. This includes strings and colors, for example. The IDE provides a single value file strings.xml when creating a project. It is important to use strings.xml to define string constants rather than include literal strings directly in views and code to support multiple languages.
Folders: assets The assets folder is initially empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager.
Folders: bin The bin folder is the output directory of the build. This is where you can find the final .apk file and other compiled resources..
XML It will be helpful to understand a little of the format used in the resource files, at least the ones in text format. These all use a language scheme called XML. This looks very much like HTML, if you are familiar with that important web language. Here is a short example from the HelloName application: <resources> <string name="app_name">HelloName</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">Hello There</string> <string name="title">Enter Your Name!</string> <string name="buttonLabel">Click This Button</string> </resources>
XML XML is built around tags.These are the pieces of text surrounded by ‘<’ and ‘>’. A start tag begins with a word, after the opening ‘<’. An end tag begins with ‘/’ and then a word after the opening ‘<’. In both kinds of tags, the word is called the tag name. Other than optional spaces, an end tag contains nothing else before the closing ‘>’: <resources> <string name="app_name">HelloName</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">Hello There</string> <string name="title">Enter Your Name!</string> <string name="buttonLabel">Click This Button</string> </resources>
XML Note that each start tag is matched with an end tag having the same name. The part of the document from one start tag to its matching end tag is called an element. As can be seen below, an element may contain plain text between the two tags, as is the case with all the ‘string’ tags. But, other elements may be contained between a start and end tag, such as with the ‘resources’ tag. <resources> <string name="app_name">HelloName</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">Hello There</string> <string name="title">Enter Your Name!</string> <string name="buttonLabel">Click This Button</string> </resources>
XML There are several rules for the arrangements of tags in all XML documents: As mentioned above, each start tag must have a matching end tag. If a start tag appears inside an element, then its matching end tag must also be inside the element. There is one element that contains all the other elements in the document. This is called the root element. <resources> <string name="app_name">HelloName</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">Hello There</string> <string name="title">Enter Your Name!</string> <string name="buttonLabel">Click This Button</string> </resources>
XML There is one exception to the first rule, the so-called empty tags. This is used for tags where there will never be any other content between the start and the end tag. For example, the <br> tag in HTML indicates a line break. It would make no sense to put anything between <br> and </br>. So, XMl allows a shortcut: <br />. This empty tag takes the place of the combination of start and end tags. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <Button android:id="@+id/buttonEquals" android:layout_width="65dp" android:layout_height="50dp" android:text="@string/btnStrEquals" /> </RelativeLayout>
Editors Editors are the tool used to modify and add to an application. Different editors are provided for different types of files. Java Editor This editor will be familiar to most developers who have used the Eclipse environment before. As the name implies, it is used to edit Java source code. GUI Editor This is the editor used to edit views, such as activity_main.xml. found in the layout folder. These views are created from descriptions in XML files. One approach to creating and changing these views is to edit the XML directly.
Editors Resources Editor This editor is used to edit files in the ‘values’ subfolder of the ‘res’ folder, such as strings.xml or styles.xml. XML Editor This is the editor used to edit XML code:
Editors AndroidManifest Editor This editor is used to edit the manifest file editor. It is similar to the properties editor, though there are extra fields that identify important Package, Version and Installation details.
Logs When your Android App is running on a connected device or an emulator you can capture real-time log messages in the LogCat (for Log Catalog) window. The messages are itemized in the LogCat window by Log Level, TimeStamp, Process ID, Task ID, Application Name (if not generated by the OS), Tag String and Text Message String. The messages are color-coded in the LogCat window like so:
Logs Initially, you will probably need to open the LogCat window for the first time by going to the Windows menu, then selecting “Show View” and then “Other” and then “Android” and then “LogCat”. There are 5 standard Levels of Logs being generated continuously by the runtime environment, each with their own color. And for your own debugging purposes you can generate your own Logs with your own Tag strings and Message strings with the following commands: Debug – Blue – Command: Log.d(TagString, MessageString); Error Logs – Red – Command: Log.e(TagString, MessageString); Info Logs – Green – Command: Log.i(TagString, MessageString); Verbose Logs – Black – Command: Log.v(TagString, MessageString); Warning Logs – Orange – Command: Log.w(TagString, MessageString);