diff options
| author | Scott Main <smain@google.com> | 2011-02-06 15:21:49 -0800 |
|---|---|---|
| committer | Scott Main <smain@google.com> | 2011-02-07 09:35:14 -0800 |
| commit | 35319945af17fb7a276e3dd6a2dc22d8979608de (patch) | |
| tree | ba6e77bade1cd388563982c10f5a804ed7fcecd5 /samples/SearchableDictionary | |
| parent | 89170f60d4c1b3af665b4d01e39662a1193755b1 (diff) | |
| download | android_development-35319945af17fb7a276e3dd6a2dc22d8979608de.tar.gz android_development-35319945af17fb7a276e3dd6a2dc22d8979608de.tar.bz2 android_development-35319945af17fb7a276e3dd6a2dc22d8979608de.zip | |
update the SearchableDictionary sample to use SearchView in the Action Bar
and set the main activity to use launchMode="singleTop"
Change-Id: I72bd13bb8250d0b00a713fe2980a5ad692f2cb9e
Diffstat (limited to 'samples/SearchableDictionary')
5 files changed, 38 insertions, 7 deletions
diff --git a/samples/SearchableDictionary/AndroidManifest.xml b/samples/SearchableDictionary/AndroidManifest.xml index 7ef031d0e..5d835ee97 100644 --- a/samples/SearchableDictionary/AndroidManifest.xml +++ b/samples/SearchableDictionary/AndroidManifest.xml @@ -21,14 +21,14 @@ android:versionCode="2" android:versionName="2.0"> - <uses-sdk android:minSdkVersion="4" /> + <uses-sdk android:minSdkVersion="11" /> <application android:label="@string/app_name" android:icon="@drawable/ic_dictionary"> <!-- The default activity of the app; displays search results. --> <activity android:name=".SearchableDictionary" - android:theme="@android:style/Theme.NoTitleBar"> + android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> @@ -48,8 +48,7 @@ </activity> <!-- Displays the definition of a word. --> - <activity android:name=".WordActivity" - android:theme="@android:style/Theme.NoTitleBar" /> + <activity android:name=".WordActivity" /> <!-- Provides search suggestions for words and their definitions. --> <provider android:name=".DictionaryProvider" diff --git a/samples/SearchableDictionary/res/menu/options_menu.xml b/samples/SearchableDictionary/res/menu/options_menu.xml index 2aa7cf211..c35c7d4b9 100644 --- a/samples/SearchableDictionary/res/menu/options_menu.xml +++ b/samples/SearchableDictionary/res/menu/options_menu.xml @@ -20,5 +20,7 @@ <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:title="@string/menu_search" - android:icon="@drawable/ic_menu_search" /> + android:icon="@drawable/ic_menu_search" + android:showAsAction="ifRoom" + android:actionViewClass="android.widget.SearchView" /> </menu> diff --git a/samples/SearchableDictionary/res/values/strings.xml b/samples/SearchableDictionary/res/values/strings.xml index ee628ced0..e7aa7c13b 100644 --- a/samples/SearchableDictionary/res/values/strings.xml +++ b/samples/SearchableDictionary/res/values/strings.xml @@ -34,7 +34,7 @@ <string name="settings_description">Definitions of words</string> <!-- General instructions in the main activity. --> - <string name="search_instructions">Press the search key to look up a word</string> + <string name="search_instructions">Use the search box in the Action Bar to look up a word</string> <!-- Shown above search results when we receive a search request. --> <plurals name="search_results"> diff --git a/samples/SearchableDictionary/src/com/example/android/searchabledict/SearchableDictionary.java b/samples/SearchableDictionary/src/com/example/android/searchabledict/SearchableDictionary.java index ef938f8a6..a83ebce89 100644 --- a/samples/SearchableDictionary/src/com/example/android/searchabledict/SearchableDictionary.java +++ b/samples/SearchableDictionary/src/com/example/android/searchabledict/SearchableDictionary.java @@ -17,7 +17,9 @@ package com.example.android.searchabledict; import android.app.Activity; +import android.app.ActionBar; import android.app.SearchManager; +import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; @@ -28,6 +30,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; +import android.widget.SearchView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; @@ -50,8 +53,19 @@ public class SearchableDictionary extends Activity { mTextView = (TextView) findViewById(R.id.text); mListView = (ListView) findViewById(R.id.list); - Intent intent = getIntent(); + handleIntent(getIntent()); + } + + @Override + protected void onNewIntent(Intent intent) { + // Because this activity has set launchMode="singleTop", the system calls this method + // to deliver the intent if this actvity is currently the foreground activity when + // invoked again (when the user executes a search from this activity, we don't create + // a new instance of this activity, so the system delivers the search intent here) + handleIntent(intent); + } + private void handleIntent(Intent intent) { if (Intent.ACTION_VIEW.equals(intent.getAction())) { // handles a click on a search suggestion; launches activity to show word Intent wordIntent = new Intent(this, WordActivity.class); @@ -115,6 +129,12 @@ public class SearchableDictionary extends Activity { public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); + + SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); + SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); + searchView.setIconifiedByDefault(false); + return true; } diff --git a/samples/SearchableDictionary/src/com/example/android/searchabledict/WordActivity.java b/samples/SearchableDictionary/src/com/example/android/searchabledict/WordActivity.java index 00dc270c4..577534c4c 100644 --- a/samples/SearchableDictionary/src/com/example/android/searchabledict/WordActivity.java +++ b/samples/SearchableDictionary/src/com/example/android/searchabledict/WordActivity.java @@ -17,6 +17,8 @@ package com.example.android.searchabledict; import android.app.Activity; +import android.app.ActionBar; +import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -35,6 +37,9 @@ public class WordActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.word); + ActionBar actionBar = getActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + Uri uri = getIntent().getData(); Cursor cursor = managedQuery(uri, null, null, null, null); @@ -67,6 +72,11 @@ public class WordActivity extends Activity { case R.id.search: onSearchRequested(); return true; + case android.R.id.home: + Intent intent = new Intent(this, SearchableDictionary.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + return true; default: return false; } |
