1page.title=Remaining Backward Compatible
2trainingnavtop=true
3previous.title=Storing and Searching for Data
4previous.link=search.html
5
6@jd:body
7
8  <div id="tb-wrapper">
9    <div id="tb">
10      <h2>This lesson teaches you to</h2>
11
12      <ul>
13        <li><a href="{@docRoot}training/search/backward-compat.html#set-sdk">Set Minimum
14        and Target API levels</a></li>
15
16        <li><a href="{@docRoot}training/search/backward-compat.html#provide-sd">Provide the Search
17        Dialog for Older Devices</a></li>
18
19        <li><a href="{@docRoot}training/search/backward-compat.html#check-ver">Check the Android Build
20        Version at Runtime</a></li>
21      </ul>
22    </div>
23  </div>
24
25  <p>The {@link android.widget.SearchView} and action bar are only available on Android 3.0 and
26  later. To support older platforms, you can fall back to the search dialog. The search dialog is a
27  system provided UI that overlays on top of your application when invoked.</p>
28
29  <h2 id="set-sdk">Set Minimum and Target API levels</h2>
30
31  <p>To setup the search dialog, first declare in your manifest that you want to support older
32  devices, but want to target Android 3.0 or later versions. When you do this, your application
33  automatically uses the action bar on Android 3.0 or later and uses the traditional menu system on
34  older devices:</p>
35  <pre>
36&lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /&gt;
37
38&lt;application&gt;
39...
40</pre>
41
42  <h2 id="provide-sd">Provide the Search Dialog for Older Devices</h2>
43
44  <p>To invoke the search dialog on older devices, call {@link
45  android.app.Activity#onSearchRequested onSearchRequested()} whenever a user selects the search
46  menu item from the options menu. Because Android 3.0 and higher devices show the
47  {@link android.widget.SearchView} in the action bar (as demonstrated in the first lesson), only versions
48  older than 3.0 call {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()} when the
49  user selects the search menu item.
50  </p>
51  <pre>
52&#64;Override
53public boolean onOptionsItemSelected(MenuItem item) {
54    switch (item.getItemId()) {
55        case R.id.search:
56            onSearchRequested();
57            return true;
58        default:
59            return false;
60    }
61}
62</pre>
63
64  <h2 id="check-ver">Check the Android Build Version at Runtime</h2>
65
66  <p>At runtime, check the device version to make sure an unsupported use of {@link
67  android.widget.SearchView} does not occur on older devices. In our example code, this happens in
68  the {@link android.app.Activity#onCreateOptionsMenu onCreateOptionsMenu()} method:</p>
69  <pre>
70&#64;Override
71public boolean onCreateOptionsMenu(Menu menu) {
72
73    MenuInflater inflater = getMenuInflater();
74    inflater.inflate(R.menu.options_menu, menu);
75
76    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) {
77        SearchManager searchManager =
78                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
79        SearchView searchView =
80                (SearchView) menu.findItem(R.id.search).getActionView();
81        searchView.setSearchableInfo(
82                searchManager.getSearchableInfo(getComponentName()));
83        searchView.setIconifiedByDefault(false);
84    }
85    return true;
86}
87</pre>
88