blob: 11473a03890ac5f57f3045038050a3aa221342e7 [file] [log] [blame]
page.title=Remaining Backward Compatible
previous.title=Storing and Searching for Data
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<li><a href="{@docRoot}training/search/backward-compat.html#set-sdk">Set Minimum
and Target API levels</a></li>
<li><a href="{@docRoot}training/search/backward-compat.html#provide-sd">Provide the Search
Dialog for Older Devices</a></li>
<li><a href="{@docRoot}training/search/backward-compat.html#check-ver">Check the Android Build
Version at Runtime</a></li>
<p>The {@link android.widget.SearchView} and action bar are only available on Android 3.0 and
later. To support older platforms, you can fall back to the search dialog. The search dialog is a
system provided UI that overlays on top of your application when invoked.</p>
<h2 id="set-sdk">Set Minimum and Target API levels</h2>
<p>To setup the search dialog, first declare in your manifest that you want to support older
devices, but want to target Android 3.0 or later versions. When you do this, your application
automatically uses the action bar on Android 3.0 or later and uses the traditional menu system on
older devices:</p>
&lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /&gt;
<h2 id="provide-sd">Provide the Search Dialog for Older Devices</h2>
<p>To invoke the search dialog on older devices, call {@link onSearchRequested()} whenever a user selects the search
menu item from the options menu. Because Android 3.0 and higher devices show the
{@link android.widget.SearchView} in the action bar (as demonstrated in the first lesson), only versions
older than 3.0 call {@link onOptionsItemSelected()} when the
user selects the search menu item.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
return true;
return false;
<h2 id="check-ver">Check the Android Build Version at Runtime</h2>
<p>At runtime, check the device version to make sure an unsupported use of {@link
android.widget.SearchView} does not occur on older devices. In our example code, this happens in
the {@link onCreateOptionsMenu()} method:</p>
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(;
return true;