Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Introduction to Android (Part.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Introduction to Android (Part."— Presentation transcript:

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Introduction to Android (Part 4) Folders, XML, Editors, Logs

2 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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 1 Folders

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: src

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: gen

5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: res

6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: drawable

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: layout

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: values

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: assets

10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 The bin folder is the output directory of the build. This is where you can find the final.apk file and other compiled resources.. Folders: bin

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: HelloName Hello world! Settings Hello There Enter Your Name! Click This Button XML

12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 XML is built around tags.These are the pieces of text surrounded by ‘ ’. A start tag begins with a word, after the opening ‘ ’: HelloName Hello world! Settings Hello There Enter Your Name! Click This Button XML

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. HelloName Hello world! Settings Hello There Enter Your Name! Click This Button XML

14 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. HelloName Hello world! Settings Hello There Enter Your Name! Click This Button XML

15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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 tag in HTML indicates a line break. It would make no sense to put anything between and. So, XMl allows a shortcut:. This empty tag takes the place of the combination of start and end tags. <RelativeLayout xmlns: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" /> XML

16 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

18 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. Editors

19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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); Logs


Download ppt "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Introduction to Android (Part."

Similar presentations


Ads by Google