Supporting Different Devices

Android devices come in many shapes and sizes all around the world. With a wide range of device types, you have an opportunity to reach a huge audience with your app. In order to be as successful as possible on Android, your app needs to adapt to various device configurations. Some of the important variations that you should consider include different languages, screen sizes, and versions of the Android platform.
This class teaches you how to use basic platform features that leverage alternative resources and other features so your app can provide an optimized user experience on a variety of Android-compatible devices, using a single application package (APK).

Supporting Different Languages

Create a project using the Android Studio (read Creating an Android Project), Android Studio create a res/ directory in the project. Within this res/ directory are sub directories for various resource types. There are also a few default files such as res/values/strings.xml, which holds your string values.

Create Locale Directories and String Files

To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO language code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code “es”. Android loads the appropriate resources according to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:


Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user’s device.
For example, the following are some different string resource files for different languages.
English (default locale), /values/strings.xml:

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

Spanish, /values-es/strings.xml:

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

French, /values-fr/strings.xml:

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

Note: You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable.

Use the String Resources

You can reference your string resources in your source code and other XML files using the resource name defined by the <string> element’s name attribute.
In your source code, 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.
For example:

// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);
// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);

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.
For example:

    android:text="@string/hello_world" />

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.

  • There are four generalized sizes: small, normal, large, xlarge
  • And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

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. Each layout should be saved into the appropriate resources directory, named with a -<screen_size> suffix. For example, a unique layout for large screens should be saved underres/layout-large/.

Note: Android automatically scales your layout in order to properly fit the screen. Thus, your layouts for different screen sizes don’t need to worry about the absolute size of UI elements but instead focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).

For example, this project includes a default layout and an alternative layout for large screens:


The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.
Simply reference the layout file in your app as usual:

 protected void onCreate(Bundle savedInstanceState) {

As another example, here’s a project with an alternative layout for landscape orientation:


By default, the layout/main.xml file is used for portrait orientation.
If you want to provide a special layout for landscape, including while on large screens, then you need to use both the large and land qualifier:

        layout/              # default (portrait)
        layout-land/         # landscape
        layout-large/        # large (portrait)
        layout-large-land/   # 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.
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

This means that if you generate a 200×200 image for xhdpi devices, you should generate the same resource in 150×150 for hdpi, 100×100 for mdpi, and 75×75 for ldpi devices.
Then, place the files in the appropriate drawable resource directory:


Any time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen’s density.

Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.

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.

Tip: In order to provide the best features and functionality across several Android versions, you should use the Android Support Library in your app, which allows you to use several recent platform APIs on older versions.

Specify Minimum and Target API Levels

The AndroidManifest.xml file describes details about your app and identifies which versions of Android it supports. Specifically, the minSdkVersion and targetSdkVersion attributes for the <uses-sdk element identify the lowest API level with which your app is compatible and the highest API level against which you’ve designed and tested your app.
For example:

<manifest xmlns:android="" ... >
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="23" />

As new versions of Android are released, some style and behaviors may change. To allow your app to take advantage of these changes and ensure that your app fits the style of each user’s device, you should set the targetSdkVersion value to match the latest Android version available.

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.

private void setUpActionBar() {
    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        ActionBar actionBar = getActionBar();

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. By using these built in styles and themes, your app will naturally follow the latest look and feel of Android with each new release.
To make your activity look like a dialog box:

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

To make your activity have a transparent background:

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

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

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

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

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

For more about creating and using themes, read the Styles and Themes guide.

About the author

Akshay Raj

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *