2.74k likes | 2.98k Vues
Android 7: Resources. Kirk Scott. This is a “light” unit, which just tries to consolidate some things before moving on to more complicated topics Resources came up in the very first examples This unit expands on that initial introduction
E N D
Android 7: Resources Kirk Scott
This is a “light” unit, which just tries to consolidate some things before moving on to more complicated topics • Resources came up in the very first examples • This unit expands on that initial introduction • However, it doesn’t treat this topic in complete detail or in depth
My theory on a lot of programming is that you only really learn things when you need them for a program you’re developing • Hopefully, you get just enough background on this topic here so that you can go further with it later on if you want to
This is a list of the sections in this set of overheads: • 7.1 Introduction • 7.2 Strings • 7.3 String Arrays • 7.4 More Resource Types • 7.5 IDs
7.6 Integers • 7.7 Dimensions • 7.8 Colors • 7.9 Styles • 7.10 Layouts • 7.11 Menus • 7.12 Graphics (Drawables)
7.13 Typed Arrays (of Drawables) • 7.14 Animations • 7.15 XML Files • 7.16 Raw Files • 7.17 Errors • 7.18 Designing an App • 7.19 Using the Graphical Development Tools • 7.20 Example App with Resources
The fundamental idea behind resources was introduced earlier: • Code should consist only of executable logic • Store everything else separately from it, in XML files • In the Android environment, there is a predefined directory structure for storing resources
A screenshot of the environment is given on the following overhead for MyEchoApp • It shows the explorer on the left with selected subdirectories of the res (resources) directory expanded • All of the directories and files shown are the defaults that come with any app
In essence, large parts of this unit are an introduction to the information you can find in the developer tutorials • I won’t cover all of the resource types • Instead, I’ll select some of them and run through the tutorial information on that selected subset • The table on the following overhead summarizes the selected subset of resource types
7.2 Strings • We already know about strings • However, mostly what we know consists of copy and paste • The point now is to actually read the information about string resources • The information from the tutorials starts on the following overhead
String Resources • A string resource provides text strings for your application with optional text styling and formatting. • There are three types of resources that can provide your application with strings: • String XML resource that provides a single string.
String Array XML resource that provides an array of strings. • Quantity Strings (Plurals) XML resource that carries different strings for pluralization. • All strings are capable of applying some styling markup and formatting arguments. • For information about styling and formatting strings, see the section about Formatting and Styling. • [Note that these last two types of resources are not covered in these overheads.]
String • A single string that can be referenced from the application or from other resource files (such as an XML layout). • Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). • So, you can combine string resources with other simple resources in the one XML file, under one <resources> element.
file location: • res/values/filename.xmlThe filename is arbitrary. The <string> element's name will be used as the resource ID. • compiled resource datatype: • Resource pointer to a String. • resource reference: • In Java: R.string.string_name • In XML:@string/string_name
syntax: • <?xml version="1.0" encoding="utf-8"?><resources> <string name="string_name" >text_string</string></resources>
elements: • <resources> • Required. • This must be the root node. • No attributes. • <string> • A string, which can include styling tags. • Beware that you must escape apostrophes and quotation marks. • For more information about how to properly style and format your strings see Formatting and Styling, below. • attributes: • name String. A name for the string. This name will be used as the resource ID.
example: • XML file saved at res/values/strings.xml: • <?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello!</string></resources>
This layout XML applies a string to a View: • <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" />
This application code retrieves a string: • String string = getString(R.string.hello); • You can use either getString(int) or getText(int) to retrieve a string. • getText(int) will retain any rich text styling applied to the string.
7.3 String Arrays • String Array • An array of strings that can be referenced from the application. • Note: A string array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). • As such, you can combine string array resources with other simple resources in the one XML file, under one <resources> element.
file location: • res/values/filename.xml • The filename is arbitrary. • The <string-array> element's name will be used as the resource ID. • compiled resource datatype: • Resource pointer to an array of Strings. • resource reference: • In Java: R.array.string_array_name
syntax: • <?xml version="1.0" encoding="utf-8"?><resources> <string-array name="string_array_name"> <item >text_string</item> </string-array></resources>
elements: • <resources> • Required. • This must be the root node. • No attributes. • <string-array> • Defines an array of strings. • Contains one or more <item> elements. • attributes: • name String. • A name for the array. • This name will be used as the resource ID to reference the array.
<item> • A string, which can include styling tags. • The value can be a reference to another string resource. • Must be a child of a <string-array> element. • Beware that you must escape apostrophes and quotation marks. • See Formatting and Styling, below, for information about to properly style and format your strings. • No attributes.
example: • XML file saved at res/values/strings.xml: • <?xml version="1.0" encoding="utf-8"?><resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array></resources>
This application code retrieves a string array: • Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array);
7.4 More Resource Types • The tutorials have a catch-all group of resource types which include the four next types that will be covered in these overheads • For reference purposes, the whole set is listed on the following overheads before launching into the individual ones that have been chosen for detailed presentation
More Resource Types • This page defines more types of resources you can externalize, including: • Bool XML resource that carries a boolean value. • ColorXML resource that carries a color value (a hexadecimal color). • DimensionXML resource that carries a dimension value (with a unit of measure).
ID XML resource that provides a unique identifier for application resources and components. • Integer XML resource that carries an integer value. • Integer Array XML resource that provides an array of integers. • Typed Array XML resource that provides a TypedArray (which you can use for an array of drawables).
[The Typed Array resource type will be covered right after the drawable resource type • Drawing images may be one of the first things developers might want to add to their apps • Therefore, it seems worthwhile to see how to deal with sets of images]
7.5 IDs • Out of the previous list of resource types, IDs are covered first since they are also things we’ve seen before • Just like with Strings, the main thing to notice is that we just mindlessly copied and pasted up to this point • Now is the time to read and see what’s actually going on
In particular, remember the use of the “+” sign in the syntax of the examples so far • The activity_main.xml layout file for MyEchoApp contained this line of code, for example: • android:id="@+id/edit_message" /> • This was a shortcut, where an id could be defined where needed in the layout
The following overheads from the tutorials give the whole ball of wax on IDs • They describe everything there is to know about them beyond the shortcut
ID • A unique resource ID defined in XML. • Using the name you provide in the <item> element, the Android developer tools create a unique integer in your project's R.java class, which you can use as an identifier for an application resources (for example, a View in your UI layout) or a unique integer for use in your application code (for example, as an ID for a dialog or a result code).
Note: An ID is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). • As such, you can combine ID resources with other simple resources in the one XML file, under one <resources> element. • Also, remember that an ID resources does not reference an actual resource item; • it is simply a unique ID that you can attach to other resources or use as a unique integer in your application.
file location: • res/values/filename.xmlThe filename is arbitrary. • resource reference: • In Java: R.id.name • In XML: @[package:]id/name
syntax: • <?xml version="1.0" encoding="utf-8"?><resources> <item type="id" name="id_name" /></resources>
elements: • <resources> • Required. • This must be the root node. • No attributes. • <item> • Defines a unique ID. • Takes no value, only attributes. • attributes: • type Must be "id". • name String. • A unique name for the ID.
example: • XML file saved at res/values/ids.xml: • <?xml version="1.0" encoding="utf-8"?><resources> <item type="id" name="button_ok" /> <item type="id" name="dialog_exit" /></resources>
Then, this layout snippet uses the "button_ok" ID for a Button widget: • <Button android:id="@id/button_ok" style="@style/button_style" />
Notice that the android:id value does not include the plus sign in the ID reference, because the ID already exists, as defined in the ids.xml example above. • (When you specify an ID to an XML resource using the plus sign—in the format android:id="@+id/name"—it means that the "name" ID does not exist and should be created.)
[The last example in the subsection on IDs in the tutorials isn’t very helpful, so it is not included.]
7.6 Integers • Integer • An integer defined in XML. • Note: An integer is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). • As such, you can combine integer resources with other simple resources in the one XML file, under one <resources> element.
file location: • res/values/filename.xml • The filename is arbitrary. • The <integer> element's name will be used as the resource ID. • resource reference: • In Java: R.integer.integer_nameIn XML: @[package:]integer/integer_name
syntax: • <?xml version="1.0" encoding="utf-8"?><resources> <integer name="integer_name" >integer</integer></resources>