Android の検索機能をアプリに統合する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Compose をお試しください
Jetpack Compose は、Android で推奨される UI ツールキットです。Compose で検索機能を追加する方法について説明します。
検索は Android の主要なユーザー機能です。コンテンツがデバイス上にあるかインターネット上にあるかに関係なく、ユーザーが使用できるデータを検索できる必要があります。一貫性のある検索エクスペリエンスをユーザーに提供するために、Android には、アプリに検索を実装するのに役立つ検索フレームワークが用意されています。
図 1. カスタム検索候補が表示された検索ダイアログ。
検索フレームワークは、画面の一番上に表示される検索ダイアログと、アクティビティ レイアウトに埋め込むことができる検索ウィジェット(SearchView
)という 2 つの検索入力モードを提供します。どちらのモードでも、Android システムが、検索を実行する特定のアクティビティに検索クエリを渡すことによって検索の実装をサポートします。また、検索ダイアログまたは検索ウィジェットでは、ユーザーが入力しているときに検索候補を表示させることもできます。図 1 に、オプションの検索候補が表示されている検索ダイアログの例を示します。
検索ダイアログまたは検索ウィジェットをセットアップすると、以下を行えるようになります。
- 音声検索を有効にします。
- 最近のユーザークエリに基づいて検索候補を表示します。
- アプリデータの実際の検索結果と一致するカスタム検索候補を表示する。
- アプリの検索候補をシステム全体のクイック検索ボックスで表示します。
注: 検索フレームワークは、データを検索するための API を提供していません。検索を実行するには、データに適した API を使用する必要があります。たとえば、データが SQLite データベースに保存されている場合は、android.database.sqlite
API を使用して検索を実行します。
また、アプリ内で検索インターフェースを呼び出す専用の検索ボタンがデバイスにあるとは限りません。検索ダイアログまたはカスタムのインターフェースを使用する場合は、検索インターフェースを有効化する検索ボタンを UI に用意する必要があります。詳しくは、検索ダイアログを呼び出すをご覧ください。
以下のページで、Android のフレームワークを使用して検索を実装する方法が説明されています。
- 検索インターフェースを作成する
- 検索ダイアログまたは検索ウィジェットを使用するようにアプリを設定する方法が説明されています。
- 最近のクエリに基づく候補を追加する
- 以前に使用したクエリに基づいて候補を表示する方法が説明されています。
- カスタム候補を追加する
- アプリのカスタムデータに基づいて候補を表示する方法と、それらの候補をシステム全体のクイック検索ボックスに表示する方法が説明されています。
- 検索可能性の設定
- 検索可能構成ファイルのリファレンス ドキュメント。特定の動作については、他のドキュメントの設定ファイルの説明もご覧ください。
ユーザーのプライバシーを保護する
アプリに検索を実装する場合、ユーザーのプライバシーを保護するための対策を講じます。多くのユーザーは、スマートフォンでのアクティビティ(検索を含む)を個人情報と考えています。ユーザーのプライバシーを保護するには、以下の原則を遵守する必要があります。
- サーバーに個人情報を送信しない(送信する必要がある場合はログに記録しない)
個人情報とは、名前、メールアドレス、お支払い情報などのユーザー個人を特定可能な情報や、そうした情報に合理的に関連付けることができるその他のデータのことです。サーバーのサポートが必要な検索をアプリで実装する場合、検索クエリとともに個人情報を送信しないでください。たとえば、郵便番号で店舗を検索する場合、ユーザー ID も送信する必要はないため、サーバーには郵便番号のみを送信します。個人情報を送信する必要がある場合は、ログに記録しないでください。ログに記録する必要がある場合は、データを厳重に保護し、できるだけ早く消去してください。
- 検索履歴を消去する方法をユーザーに提供する
検索フレームワークを利用すると、ユーザーが入力しているときにコンテキスト固有の候補をアプリで表示できます。ユーザーが以前のセッションで行った検索やその他のアクションに基づいて候補が表示されることもあります。ユーザーは、以前の検索が他のデバイス ユーザーに表示されないようにしたいと思っているかもしれません。以前の検索アクティビティがわかるような候補をアプリで表示する場合、ユーザーが検索履歴を消去できる方法を実装してください。SearchRecentSuggestions
を使用している場合は、clearHistory()
メソッドを呼び出すことができます。カスタム候補を実装する場合は、ユーザーが実行可能な同様の「履歴消去」メソッドをコンテンツ プロバイダで提供する必要があります。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-26 UTC。
[null,null,["最終更新日 2025-08-26 UTC。"],[],[],null,["Try the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add search functionality in Compose. \n[Search bar →](/develop/ui/compose/components/search-bar) \n\nSearch is a core user feature on Android. Users must be able\nto search any data that is available to them, whether the content is located on the device or\nthe internet. To help create a consistent search experience for users, Android provides a\nsearch framework that helps you implement search for your application. \n\n**Figure 1.** A search dialog with custom\nsearch suggestions.\n\nThe search framework offers two modes of search input: a search dialog at the top of the\nscreen or a search widget ([SearchView](/reference/android/widget/SearchView)) that you can embed in your activity\nlayout. In either case, the Android system assists your search implementation by\ndelivering search queries to a specific activity that performs searches. You can also enable\nthe search dialog or widget to provide search suggestions as the user types. Figure 1 shows an\nexample of the search dialog with optional search suggestions.\n\nOnce you set up either the search dialog or the search widget, you can do the following:\n\n- Enable voice search.\n- Provide search suggestions based on recent user queries.\n- Provide custom search suggestions that match actual results in your application data.\n- Offer your application's search suggestions in the system-wide Quick Search Box.\n\n**Note** : The search framework does *not* provide APIs to\nsearch your data. To perform a search, you need to use APIs appropriate for your data. For example,\nif your data is stored in an SQLite database, use the [android.database.sqlite](/reference/android/database/sqlite/package-summary)\nAPIs to perform searches.\n\n\u003cbr /\u003e\n\n\nAlso, there is no guarantee that a device provides a dedicated SEARCH button that invokes the\nsearch interface in your application. When using the search dialog or a custom interface, you\nmust provide a search button in your UI that activates the search interface. For more\ninformation, see [Invoke the search\ndialog](/develop/ui/views/search/search-dialog#InvokingTheSearchDialog).\n\nThe following pages show you how to use Android's framework to implement search:\n\n**[Create a search interface](/develop/ui/views/search/search-dialog)**\n: How to set up your application to use the search dialog or search widget.\n\n**[Add recent query\nsuggestions](/develop/ui/views/search/adding-recent-query-suggestions)**\n: How to provide suggestions based on queries previously used.\n\n**[Add custom suggestions](/develop/ui/views/search/adding-custom-suggestions)**\n: How to provide suggestions based on custom data from your application and offer them\n in the system-wide Quick Search Box.\n\n**[Searchable configuration](/develop/ui/views/search/searchable-config)**\n: A reference document for the searchable configuration file. The other\n documents also discuss the configuration file in terms of specific behaviors.\n\nProtect user privacy\n\nWhen you implement search in your application, take steps to protect the user's\nprivacy. Many users consider their activities on their phone---including searches---to\nbe private information. To protect users' privacy, abide by the following\nprinciples:\n\n- **Don't send personal information to servers‐and if you must, don't log it.**\n\n Personal information is any information that can personally identify your users, such as their\n names, email addresses, billing information, or other data that can be reasonably linked to such\n information. If your application implements search with the assistance of a server, avoid sending\n personal information along with the search queries. For example, if you are searching for businesses\n near a ZIP code,\n you don't need to send the user ID as well; send only the ZIP code to the server. If you must\n send personal information, avoid logging it. If you must log it, protect that data\n very carefully and erase it as soon as possible.\n- **Provide users with a way to clear their search history.**\n\n The search framework helps your application provide context-specific suggestions while the user\n types. Sometimes these\n suggestions are based on previous searches or other actions taken by the user in an earlier\n session. A user might not want previous searches to be revealed to other device users. If your\n application provides suggestions that\n can reveal previous search activities, implement a way for the user to clear their\n search history. If you are using [SearchRecentSuggestions](/reference/android/provider/SearchRecentSuggestions),\n you can call the\n [clearHistory()](/reference/android/provider/SearchRecentSuggestions#clearHistory())\n method. If you are implementing custom suggestions, you need to provide a similar \"clear history\"\n method in your content provider that the user can execute."]]