android app development 08 : support multiple devices

36
Support Different Devices Anuchit Chalothorn [email protected] 8 Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Upload: anuchit-chalothorn

Post on 17-May-2015

523 views

Category:

Technology


3 download

TRANSCRIPT

Support Different DevicesAnuchit [email protected]

8

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Supporting Different Languages

Android will run on many devices in many regions. To reach the most users, your application should handle text, audio files, numbers, currency, and graphics in ways appropriate to the locales where your application will be used.

Using Resources for Localization

The default resource set must also include any default drawables and layouts, and can include other types of resources such as animations. ● res/drawable/● res/layout/● res/anim/● res/xml/● res/raw/

Language String

Android makes this easy with a resources directory in each Android project. Within the res/ directory are subdirectories for various resource types. There are also a few default files such as res/values/strings.xml, which holds your string values.

Locale Directories and String Files

To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. eg: values-es for Spanish, values-th for Thai, values for English etc.

Locale : English

English (default locale), /values/strings.xml :

<?xml version="1.0" encoding="utf-8"?><resources> <string name="title">My Application</string> <string name="hello_world">Hello World!</string></resources>

Locale : Spanish

Spanish, /values-es/strings.xml :

<?xml version="1.0" encoding="utf-8"?><resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string></resources>

Locale : French

French, /values-fr/strings.xml :

<?xml version="1.0" encoding="utf-8"?><resources> <string name="title">Mon Application</string> <string name="hello_world">Bonjour le monde !</string></resources>

Use the String Resources

You can refer to a string resource with the syntax R.string.<string_name>. There are a variety of methods that accept a string resource this way.

Use the String Resources

// Get a string resource from your app's ResourcesString hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a stringTextView textView = new TextView(this);textView.setText(R.string.hello_world);

Use the String in XML

In other XML files, you can refer to a string resource with the syntax @string/<string_name> whenever the XML attribute accepts a string value.

Use the String in XML

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />

Workshop: Locale

Create form UI like following and make locale string for English, Vietnamese, Japanese and Thai.

Locale your graphic

You can use qualifiers with any resources include graphic in drawable directory, eg : ● /drawable for English (default locale)● /drawable-th for Thai● /drawable-vn for Vietnamese● /drawable-ja for Japanese

Workshop: Locale your graphic

Create a UI like following and make locale graphic for English, Vietnamese, Japanese and Thai.

Supporting Different Screens

Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density.

Sizes and Densities

To declare different layouts and bitmaps you'd like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.

Create Different Layouts

To optimize your user experience on different screen sizes, you should create a unique layout XML file for each screen size you want to support.

Create Different Layouts

The directory named with a -<screen_size> suffix.● /res/layout/main.xml <-- normal ● /res/layout-land/main.xml <-- landscape ● /res/layout-large/main.xml <-- for large ● /res/layout-large-land/main.xml <-- large landscape

Create Different Bitmaps

You should always provide bitmap resources that are properly scaled to each of the generalized density buckets: low, medium, high and extra-high density. This helps you achieve good graphical quality and performance on all screen densities.

Create Different Bitmaps

To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:● xhdpi: 2.0● hdpi: 1.5● mdpi: 1.0 (baseline)● ldpi: 0.75

Ref: http://developer.android.com/design/style/iconography.html

Workshop: Drawable

Make graphic resources match each screen size.

Supporting Different Platform Versions

While the latest versions of Android often provide great APIs for your app, you should continue to support older versions of Android until more devices get updated.

Specify Minimum and Target API

The AndroidManifest.xml file describes details about your app and identifies which versions of Android it supports. Specifically, the minSdkVersion and targetSdkVersion with <use-sdk element.

Specify Minimum and Target API

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /> ...</manifest>

Check System Version at Runtime

Android provides a unique code for each platform version in the Build constants class. Use these codes within your app to build conditions that ensure the code that depends on higher API levels is executed only when those APIs are available on the system.

Check System Version at Runtime

private void setUpActionBar() {// Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); }}

Use Platform Styles and Themes

Android provides user experience themes that give apps the look and feel of the underlying operating system. These themes can be applied to your app within the manifest file.

Platform Styles and Themes

To make your activity look like a dialog box:

<activity android:theme="@android:style/Theme.Dialog">

Platform Styles and Themes

To make your activity have a transparent background:To make your activity have a transparent background:

<activity android:theme="@android:style/Theme.Translucent">

Platform Styles and Themes

To apply your own custom theme defined in /res/values/styles.xml:

<activity android:theme="@style/CustomTheme">

Platform Styles and Themes

To apply a theme to your entire app (all activities), add the android:theme attribute to the <application> element:

<application android:theme="@style/CustomTheme">

End