Android SearchView

In Android there are at least two approaches to provide the search box to the user. In the previous post, I showed you how to use the search box in your application using the search dialog component. It is recommended to use the SearchView widget if your application targets the Android 3.0 and higher. The SearchView widget does not work in Android lower than 3.0. These approaches provide the same search functionality. Now I am going to teach you to provide the search box using the SearchView widget.




To provide the search box using the SearchView widget, you need to create a search configuration xml file (searchable.xml) in the res/xml/ folder, create a searchable activity, and tell the system the activity to send the search query and the activity to receive the query and handle the searches as mentioned in the first, second, and third steps in the previous post (Android search box). The SearchView widget is generally placed on the action bar as an action view. So you create a custom menu xml file (custommenu.xml in the res/menu/ folder) in which there is a SearchView widget.

<?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:orderInCategory="100"
   android:showAsAction="always"
   android:actionViewClass="android.widget.SearchView"
  
   />
  
</menu>


Then in the onCreateOptionsMenu() method of the MainActivity class, you have to inflate the custom menu, make a reference to the SearchView widget, and some settings such as setting SeachableInfo for the SearchView, expanding the search box, and displaying the search button.

package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SearchView;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//onSearchRequested();
}
public void onResume(){
super.onResume();
//onSearchRequested();
}
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, 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();
   // Sets 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;
}
}


The code in the SearchAcitivity class that receives the search query and handles the searches (mentioned in the previous post) remains unchanged.

Android-SearchView

Posted by: Dara | post date: 09-28-2014 | Subject: Android Apps Development

write programming tip



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.