page.title=Maintaining Compatibility @jd:body
Some material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. However, you can design your apps to make use of these features when running on devices that support material design and still be compatible with devices running previous releases of Android.
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:
res/values/styles.xml
.res/values-v21/styles.xml
.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.
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/my_activity.xml
is an alternative layout for
res/layout-v21/my_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/
.
The v7 Support Libraries r21 and above includes the following material design features:
Theme.AppCompat
themes.Theme.AppCompat
themes.The Theme.AppCompat
themes provide material design styles for these widgets:
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"> <!-- customize the color palette --> <item name="colorPrimary">@color/material_blue_500</item> <item name="colorPrimaryDark">@color/material_blue_700</item> <item name="colorAccent">@color/material_green_A200</item> </style>
The {@link android.support.v7.widget.RecyclerView} and {@link android.support.v7.widget.CardView} widgets are available in earlier versions of Android through the Android v7 Support Library with these limitations:
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:21.0.+' compile 'com.android.support:cardview-v7:21.0.+' compile 'com.android.support:recyclerview-v7:21.0.+' }
The following features are available only in Android 5.0 (API level 21) and above:
To preserve compatibility with earlier versions of Android, check the system {@link android.os.Build.VERSION#SDK_INT 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
. For more information, see
the <uses-sdk> API
guide.