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 Using Android XML Resources.

Similar presentations


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

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Using Android XML Resources and Editors

2 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 The resources (res) folder contains resources used by your application. You can add resource files in a variety of formats to these folders, such as bitmap images such as JPEG and PNG, and XML files for non- image resources. Or you can build non-image resources using the editors available for these folders. The Resource (res) Folder

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 You can create an entire Android project without building any of your GUI in Java code. You can build all of your GUI components using the Android Layout editor. To use this editor, open up the layout folder, and then double click on the XML file that is in that folder, which in the case shown here is “activity_salution.xml” The Resource (res) Folder

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 The Layout Editor is the tool used to modify and add GUI components to your application. You can edit the Layout components using both a GUI editor and an XML editor. Whether you use the GUI or the XML editor, both editors modify an XML file that holds the description of the Layout. They are both changes the same file, but the different editors give you a different approach to making the same changes. You can make changes in either editor and then switch back to other one to see that it has been changed also. The Layout Editor

5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Using the Palette, you can choose different types of components to add to your GUI. The components are organized in folders according to their type. And you just drag and drop them out of the folder to add them to your GUI. The Layout Editor

6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 When you click on a component, you will see its attributes (or properties) listed in a Properties window on the side. From there you can change the values of the attributes you see. The Layout Editor

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 This is the editor used to edit XML code. When looking at the XML code you are looking at all of the properties of a particular component, and you are seeing the exact attribute values and units that are used to manage that component. This is at times preferable to seeing the component only in the GUI editor because it is easy to miss details, and harder to modify some details when you are primarily in the drag and drop mode (although you can edit properties in the Properties window as well). The XML Editor

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  There are six basic units to use when identifying distance on the Android Screen: px, in, mm, pt, dp, and sp –px - Pixels - corresponds to actual pixels on the screen. –in - Inches - based on the physical size of the screen. –mm - Millimeters - based on the physical size of the screen. –pt - Points - 1/72 of an inch based on the physical size of the screen. –dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen (see next slide) –sp - Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference (see next slide). Drawing Units

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".  sp - Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen densityand user's preference. Drawing Units

10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Android projects do not use the standard Java Layout Manager. Instead, for the Android, each type of container supports a particular strategy for laying out its components. There are also standard ‘layouts’ that you can overlay onto multiple items: LinearLayout In a LinearLayout components are arranged in a row horizontally or in a column vertically. If there is not enough space, the ones on the end (right or bottom) may not show at all. More complex layouts can be constructed by putting horizontal LinearLayout’s inside vertical a LinearLayout. This lets the designer set up a rough grid of components. Android Layouts

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 RelativeLayout The RelativeLayout holds components directly but arranges components based on specified relationships among the placements of components. For example, one component can be a specified distance below another component. Or, a component can have its left edge lined up with the left edge of another component. Relationships can be specified with the edges of the layout itself. The RelativeLayout is very flexible, but can be tricky to use. TableLayout While the TableLayout can hold components directly, it is more often used by putting TableRow’s inside and then putting components inside the TableRow’s. This has somewhat the same effect as putting LinearLayout’s inside LinearLayout’s with one major difference: the components in different rows are lined up in columns. Layouts

12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 The View for the Android Calculator App is completed using the Layout Editors, and now you can implement the functionality of the app using EvaluateExpression.java as a Model, and CalculatorActivity.java as the Controller. See activity_salutation.xml See SalutationActivity.java The Android Salutation App

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 The View for the Android Simple Calculator App is completed using the Layout Editors, and now you can implement the functionality of the app using EvaluateExpression.java as a Model, and SimpleCalculatorActivity.java as the Controller. See activity_simplecalculator.xml See EvaluateExpression.java See SimpleCalculatorActivity.java The Android Simple Calculator App


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

Similar presentations


Ads by Google