Show results in current activity using SearchView

I am able to use SearchView to provide search box on the ActionBar. I used two activities. One activity displays the search box. Another activity receives the search query and displays the results. Is it possible to use only one activity to achieve the same goal?

By tyna99 asked on 2015-11-10
answer to this question

By yuk.dara

It is possible to achieve your goal. Here is how to accomplish the task.
1. In the res/xml directory, you create searchable.xml file.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android=""
android:hint="Search" >

2. In AndroidManifest.xml file, you have to make sure that your activity is able to receive search intent and searchable using meta-data tag. Here is an example:
android:launchMode="singleTop" >
<action android:name="android.intent.action.SEARCH" />
android:resource="@xml/searchable" />
android:value="com.example.dara.kloudapp.LibActivity" />

3. In the res/menu directory, you create custommenu.xml file that contains a SearchView.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""
<item android:id="@+id/searchitem"
android:title="search box"

4. In the onCreateOptionsMenu of the activity class, you write code to configure the SearchView as below.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(;
// Set the SearchableInfo for this SearchView searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName()));
// Expand the search widget
// Show the submit button

return true;

5. You add the following methods to the activity class.
//If you set android:launchMode to "singleTop",
//intent is passed here
protected void onNewIntent(Intent intent) {
private void getQuery(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// call a method to perform data search here

6. You call the following methods from the onCreate method of the activity class.

SearchVIew example

2015-11-10 Reply

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.