blob: 49ef7f716256452f8dfa020042e6d484e956bc34 [file] [log] [blame]
page.title=Maintaining Compatibility
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<li><a href="#Theme">Define Alternative Styles</a></li>
<li><a href="#Layouts">Provide Alternative Layouts</a></li>
<li><a href="#SupportLib">Use the Support Library</a></li>
<li><a href="#CheckVersion">Check the System Version</a></li>
<h2>You should also read</h2>
<li><a href="">Material design specification</a></li>
<li><a href="{@docRoot}design/material/index.html">Material design on Android</a></li>
<p>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.</p>
<h2 id="Theme">Define Alternative Styles</h2>
<p>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:</p>
<li>Define a theme that inherits from an older theme (like Holo) in
<li>Define a theme with the same name that inherits from the material theme in
<li>Set this theme as your app's theme in the manifest file.</li>
<p class="note"><strong>Note:</strong>
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.
<h2 id="Layouts">Provide Alternative Layouts</h2>
<p>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.</p>
<p>Create your layout files for Android 5.0 (API level 21) inside <code>res/layout-v21/</code> and
your alternative layout files for earlier versions of Android inside <code>res/layout/</code>.
For example, <code>res/layout/my_activity.xml</code> is an alternative layout for
<p>To avoid duplication of code, define your styles inside <code>res/values/</code>, modify the
styles in <code>res/values-v21/</code> for the new APIs, and use style inheritance, defining base
styles in <code>res/values/</code> and inheriting from those in <code>res/values-v21/</code>.</p>
<h2 id="SupportLib">Use the Support Library</h2>
<p>The <a href="{@docRoot}tools/support-library/features.html#v7">v7 Support Libraries</a>
r21 and above includes the following material design features:</p>
<li><a href="{@docRoot}training/material/theme.html">Material design styles</a> for some system
widgets when you apply one of the <code>Theme.AppCompat</code> themes.</li>
<li><a href="{@docRoot}training/material/theme.html#ColorPalette">Color palette theme attributes</a>
in the <code>Theme.AppCompat</code> themes.</li>
<li>The {@link} widget to <a
href="{@docRoot}training/material/lists-cards.html#RecyclerView">display data
<li>The {@link} widget to <a
href="{@docRoot}training/material/lists-cards.html#CardView">create cards</a>.</li>
<li>The {@link} class to <a
href="{@docRoot}training/material/drawables.html#ColorExtract">extract prominent colors from
<h3>System widgets</h3>
<p>The <code>Theme.AppCompat</code> themes provide material design styles for these widgets:</p>
<li>{@link android.widget.EditText}</li>
<li>{@link android.widget.Spinner}</li>
<li>{@link android.widget.CheckBox}</li>
<li>{@link android.widget.RadioButton}</li>
<li>{@link android.widget.CheckedTextView}</li>
<h3>Color Palette</h3>
<p>To obtain material design styles and customize the color palette with the Android v7 Support
Library, apply one of the <code>Theme.AppCompat</code> themes:</p>
&lt;!-- extend one of the Theme.AppCompat themes -->
&lt;style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
&lt;!-- customize the color palette -->
&lt;item name="colorPrimary">@color/material_blue_500&lt;/item>
&lt;item name="colorPrimaryDark">@color/material_blue_700&lt;/item>
&lt;item name="colorAccent">@color/material_green_A200&lt;/item>
<h3>Lists and Cards</h3>
<p>The {@link} and {@link} widgets are available in earlier versions of Android through
the Android v7 Support Library with these limitations:</p>
<li>{@link} falls back to a programmatic shadow implementation
using additional padding.</li>
<li>{@link} does not clip its children views that intersect
with rounded corners.</li>
<p>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 <a
href="{@docRoot}/sdk/installing/studio-build.html#dependencies">Gradle dependency</a>:</p>
dependencies {
compile ''
compile ''
compile ''
<h2 id="CheckVersion">Check the System Version</h2>
<p>The following features are available only in Android 5.0 (API level 21) and above:</p>
<li>Activity transitions</li>
<li>Touch feedback</li>
<li>Reveal animations</li>
<li>Path-based animations</li>
<li>Vector drawables</li>
<li>Drawable tinting</li>
<p>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
// Check if we're running on Android 5.0 or higher
// Call some material design APIs here
} else {
// Implement this feature without material design
<p class="note"><strong>Note:</strong> To specify which versions of Android your app supports,
use the <code>android:minSdkVersion</code> and <code>android:targetSdkVersion</code>
attributes in your manifest file. To use the material design features in Android 5.0, set
the <code>android:targetSdkVersion</code> attribute to <code>21</code>. For more information, see
the <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt; API