html5-img
1 / 256

Android 7: Resources

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

gella
Download Presentation

Android 7: Resources

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android 7: Resources Kirk Scott

  2. 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

  3. 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

  4. 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

  5. 7.6 Integers • 7.7 Dimensions • 7.8 Colors • 7.9 Styles • 7.10 Layouts • 7.11 Menus • 7.12 Graphics (Drawables)

  6. 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

  7. 7.1 Introduction

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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.

  13. 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.]

  14. 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.

  15. 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

  16. syntax: • <?xml version="1.0" encoding="utf-8"?><resources>    <string        name="string_name"        >text_string</string></resources>

  17. 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.

  18. example: • XML file saved at res/values/strings.xml: • <?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello!</string></resources>

  19. This layout XML applies a string to a View: • <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" />

  20. 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.

  21. 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.

  22. 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

  23. syntax: • <?xml version="1.0" encoding="utf-8"?><resources>    <string-array        name="string_array_name">        <item            >text_string</item>    </string-array></resources>

  24. 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.

  25. <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.

  26. 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>

  27. This application code retrieves a string array: • Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array);

  28. 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

  29. 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).

  30. 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).

  31. [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]

  32. 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

  33. 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

  34. 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

  35. 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).

  36. 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.

  37. file location: • res/values/filename.xmlThe filename is arbitrary. • resource reference: • In Java: R.id.name • In XML: @[package:]id/name

  38. syntax: • <?xml version="1.0" encoding="utf-8"?><resources>    <item        type="id"        name="id_name" /></resources>

  39. 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.

  40. 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>

  41. Then, this layout snippet uses the "button_ok" ID for a Button widget: • <Button android:id="@id/button_ok"    style="@style/button_style" />

  42. 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.)

  43. [The last example in the subsection on IDs in the tutorials isn’t very helpful, so it is not included.]

  44. 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.

  45. 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

  46. syntax: • <?xml version="1.0" encoding="utf-8"?><resources>    <integer        name="integer_name"        >integer</integer></resources>

More Related