HomeAndroidAndroid Maintaining Compatibility

Android Maintaining Compatibility

Some of the Android material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. You can still make your app compatible with devices running previous releases of Android and also take the full advantage of the features of the material design.

Maintaining Compatibility

You can configure your app to use the material theme on devices that support it and revert to an older theme on devices running earlier versions of Android.

Apply Alternative Styles

  1. Define a theme that inherits from an older theme (like Holo) in res/values/styles.xml.
  2. Define a theme with the same name that inherits from the material theme in res/values-v21/styles.xml.
  3. Set this theme as your app’s theme in the manifest file.

Note: If your app uses the material theme but does not provide an alternative theme in this manner, your app will not run on versions of Android earlier than 5.0.

Provide Alternative Layouts

If the layouts that you design according to the material design guidelines do not use any of the new XML attributes introduced in Android 5.0 (API level 21), they will work on previous versions of Android. Otherwise, you can provide alternative layouts. You can also provide alternative layouts to customize how your app looks on earlier versions of Android.

Create your layout files for Android 5.0 (API level 21) inside res/layout-v21/ and your alternative layout files for earlier versions of Android inside res/layout/. For example, res/layout/main_activity.xml is an alternative layout for res/layout-v21/main_activity.xml.

To avoid duplication of code, define your styles inside res/values/, modify the styles in res/values-v21/ for the new APIs, and use style inheritance, defining base styles in res/values/ and inheriting from those in res/values-v21/.

Use the Support Library

The v7 Support Libraries r21 and above includes the following material design features:

Updated support library versions may provide additional support for material design features. See the Support Library revision history for more information.

Color Palette

To obtain material design styles and customize the color palette with the Android v7 Support Library, apply one of the Theme.AppCompat themes:

<!-- extend one of the Theme.AppCompat themes -->
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
  <item name="primaryColor">@color/primaryColor</item>
  <item name="primaryDarkColor">@color/primaryDarkColor</item>
  <item name="accentColor">@color/accentColor</item>
</style>

Dependencies

To use these features in versions of Android earlier than 5.0 (API level 21), include the Android v7 Support Library in your project as a Gradle dependency:

dependencies {
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:cardview-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
}

Check the System Version

The following framework features are available only in Android 5.0 (API level 21) and above:

  • Activity transitions
  • Touch feedback
  • Reveal animations
  • Path-based animations

To preserve compatibility with earlier versions of Android, check the system version at runtime before you invoke the APIs for any of these features:

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
} else {
    // Implement this feature without material design
}

Note: To specify which versions of Android your app supports, use the android:minSdkVersion and android:targetSdkVersion attributes in your manifest file. To use the material design features in Android 5.0, set the android:targetSdkVersion attribute to 21.

RELATED ARTICLES

Most Popular