Maintaining compatibility This lesson teaches
- Define Alternative Styles
- Provide alternative Layouts
- Use the support Library
- Check the System Version
You should also read
- Material Design Specification
- Material Design on Android
Some Material design features like the material theme and custom activity transitions is only available on Android 5.0 (A PI level) and above. However, you can design your apps to make use of the these features when running on devices the support material design and S Till is compatible with devices running previous releases of Android.
Define Alternative Styles
You can configure your app for use the material theme on devices that support it and revert to a older theme on devices RU Nning earlier versions of Android:
- Define a theme that inherits from the older theme (like Holo) in
res/values/styles.xml
.
- Define a theme with the same name is inherits from the material theme in
res/values-v21/styles.xml
.
- Set this theme as your app's theme in the manifest file.
Note: If your app uses the material theme but does not provide a alternative theme in this manner, your app won't run on ver Sions of Android earlier than 5.0.
Provide alternative Layouts
If the layouts you design according to the material design guidelines does not use any of the new XML attributes Introd Uced in Android 5.0 (API level), they'll work on previous versions of Android. Otherwise, you can provide alternative layouts. can also provide alternative layouts to customize what your app looks on earlier versions of Android.
Create your layout files for Android 5.0 (API level) inside and res/layout-v21/
your alternative layout files for earlier version S of Android inside res/layout/
. For example, a res/layout/my_activity.xml
alternative layout for res/layout-v21/my_activity.xml
.
To avoid duplication of code, define your styles inside res/values/
, modify the styles on for res/values-v21/
the new APIs, and use style Inheritance, defining base styles in and res/values/
inheriting from those in res/values-v21/
.
Use the support Library
The V7 support Libraries R21 and above includes the following material design features:
- Material design styles for some system widgets if you apply one of the
Theme.AppCompat
themes.
- Color Palette Theme attributes in the
Theme.AppCompat
themes.
- The
RecyclerView
widget to display data collections.
- The
CardView
widget to create cards.
- The
Palette
class to extract prominent colors from images.
System Widgets
The Theme.AppCompat
themes provide material design styles for these widgets:
EditText
Spinner
CheckBox
RadioButton
SwitchCompat
CheckedTextView
Color Palette
To obtain material design styles and customize the color palette with the Android v7 support Library, apply one of the Themes:
<!--extend One of the Theme.appcompat themes--
<stylename="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>
Lists and Cards
The RecyclerView
and CardView
widgets is available in earlier versions of Android through the Android V7 support Library with the SE Limitations:
CardView
Falls back to a programmatic shadow implementation using additional padding.
CardView
Does not clip it children views the intersect with rounded corners.
Dependencies
To use these features in versions of Android earlier than 5.0 (API level), include the Android V7 support Library in yo ur project as a Gradle dependency:
{
' com.android.support:appcompat-v7:21.0.+ '
' com.android.support:cardview-v7:21.0.+ '
' com.android.support:recyclerview-v7:21.0.+ '
}
Check the System Version
The following features is available only on Android 5.0 (API level) and above:
- Activity transitions
- Touch Feedback
- Reveal Animations
- path-based Animations
- Vector Drawables
- Drawable Tinting
To preserve compatibility with earlier versions of Android, check the system at version
runtime before you invoke the APIs F Or any of these features:
//Check if we ' re running on Android 5.0 or higher < br> if ( build version sdk_int >= build version_codes lollipop {
//call some material design APIs here
} else { Span class= "PLN" >
//Implement this feature without material design Span class= "PLN" >
}
Note: To specify which versions of Android your apps supports, use the android:minSdkVersion
and android:targetSdkVersion
attributes in your manifest file. To use the material design features on Android 5.0, set the android:targetSdkVersion
attribute to 21
. For more information, see the <uses-sdk> API Guide.
Material Design series Seventh--maintaining compatibility