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

yuk.dara
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="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search" >
</searchable>

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:
<activity
android:name=".LibContentActivity"
android:label="@string/title_activity_lib_content"
android:launchMode="singleTop" >
<intent-filter><br>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data<br>
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.dara.kloudapp.LibActivity" />
</activity>

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="http://schemas.android.com/apk/res/android"
>
<item android:id="@+id/searchitem"
android:visible="true"
android:title="search box"
android:orderInCategory="100"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
</menu>


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(R.menu.custommenu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.searchitem).getActionView();
searchView.setBackgroundColor(Color.parseColor("#086A87"));
// Set the SearchableInfo for this SearchView searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName()));
// Expand the search widget
searchView.setIconifiedByDefault(false);
// Show the submit button
searchView.setSubmitButtonEnabled(true);

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) {
setIntent(intent);
getQuery(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.
        getQuery(getIntent());
onSearchRequested();

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.