Presentation is loading. Please wait.

Presentation is loading. Please wait.

Resources. Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized)

Similar presentations


Presentation on theme: "Resources. Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized)"— Presentation transcript:

1 Resources

2 Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized) independently from the code. Externalizing resources also allows you to provide alternative resources that support different locales (languages/countries) and different device configurations (e.g., screen sizes/orientation). Resources are organized in a project’s res/ directory, using various sub-directories that group resources by type and configuration. Slide 2©SoftMoore Consulting

3 File Hierarchy for a Simple Project app/src/main java/ edu/citadel/android/hello/MainActivity.java res/ drawable/ drawable-hdpi drawable-mdpi drawable-xhdpi drawable-xxhdpi layout/ activity_main.xml values/ colors.xml dimens.xml strings.xml styles.xml values-w820dp/ AndroidManifest.xml Slide 3©SoftMoore Consulting

4 Alternative Resources For any type of resource, you can specify default and multiple alternative resources for your application: –default resources: used regardless of the device configuration or when there are no alternative resources that match the current configuration. –alternative resources: for use with a specific configuration. To specify that a group of resources are for a specific configuration, append an appropriate configuration qualifier to the directory name. Example/ –default UI layout is saved in the res/layout/ directory –alternate UI layout for landscape orientation is saved in the res/layout-land/ directory Slide 4©SoftMoore Consulting

5 Example: Alternative Resources Slide 5©SoftMoore Consulting Figure 1. Two different devices, both using default resources. Figure 2. Two different devices, one using alternative resources.

6 Resource Directories (subdirectories of res/ ) anim/ – XML files that define tween animations color/ – XML files that define a state list of colors (e.g., button state can be pressed, focused, or neither) drawable/ – Bitmap files (.png,.9.png,.jpg,.gif ) or XML files that are compiled into drawable resource subtypes layout/ – XML files that define a user interface layout menu/ – XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu raw/ – Arbitrary files to save in their raw form Slide 6©SoftMoore Consulting

7 Resource Directories (subdirectories of res/ – continued) values/ – XML files that contain simple values, such as strings, integers, and colors. Examples include –arrays.xml for resource arrays –colors.xml for color values –dimens.xml for dimension values –strings.xml for string values –styles.xml for styles xml/ – Arbitrary XML files that can be read at runtime Slide 7©SoftMoore Consulting

8 Specifying Configuration-Specific Alternatives Create a new directory in res/ named in the form -. – is the directory name of the corresponding default resources. – is a name that specifies an individual configuration for which these resources are to be used. You can append more than one. Separate each one with a dash. Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files. Slide 8©SoftMoore Consulting

9 Example: Specifying Configuration-Specific Alternatives res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png Slide 9©SoftMoore Consulting

10 Configuration Qualifier Names for Alternative Resource Directories MCC and MNC – mobile country code/mobile network code (e.g., to include country-specific legal resources) Language or both language and region – e.g., en, fr, en-rUS, fr-rFR, fr-rCA, etc. Screen size – small, normal, large, xlarge (e.g., tablets) Screen orientation – port, land Screen pixel density (dpi) – ldpi, mdpi, hdpi, xhdpi, nodpi Keyboard availability – keysexposed, keyshidden, keyssoft Platform version (API Level) – e.g., v3, v4, v7, etc. Slide 10©SoftMoore Consulting

11 String Resources A string is a simple resource that is referenced using the value provided in the name attribute. Example (file strings.xml in directory res/values ) Hello Android Hello, Android! Settings Slide 11©SoftMoore Consulting

12 Escaping Apostrophes and Quotes An apostrophes or a quote in a string resource must either be escaped (e.g., using “ \ ”) or else the whole string must be enclosed in the other type of quote. Examples "This'll work" This\'ll also work This doesn't work Slide 12©SoftMoore Consulting

13 Accessing Resources When your application is compiled, the Android Asset Packaging Tool ( aapt ) generates class R, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R.drawable.icon ). This integer is the resource ID that you can use to retrieve your resource. There are two ways you can access a resource: –In code: Use a static integer from a sub-class of your R class (e.g., R.string.hello ) –In XML: Use a special XML syntax that also corresponds to the resource ID defined in your R class (e.g., @string/hello ) Slide 13©SoftMoore Consulting

14 Example: Accessing Resources Accessing resources within code ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage); String submit = getString(R.string.submit); Accessing Resources from XML <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/submit" /> Slide 14©SoftMoore Consulting

15 String Array Resources In addition to simple strings, it is possible to declare an array of strings as a resource. Example (in res/strings.xml ) Mercury Venus Earth Mars Accessing the string array within the Java code Resources res = getResources();getResources() String[] planets = res.getStringArray(R.array.planets_array);getStringArray Slide 15©SoftMoore Consulting

16 Relevant Links Application Resources http://developer.android.com/guide/topics/resources/index.html Providing Resources http://developer.android.com/guide/topics/resources/providing-resources.html Localizing with Resources http://developer.android.com/guide/topics/resources/localization.html String Resources http://developer.android.com/guide/topics/resources/string-resource.html How to Localize an Android Application http://code.tutsplus.com/tutorials/how-to-localize-an-android-application--cms-22154 Slide 16©SoftMoore Consulting


Download ppt "Resources. Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized)"

Similar presentations


Ads by Google