Creating a Search Interface in Android

When you’re ready to add search functionality to your application, Android helps you implement the user interface with either a search dialog that appears at the top of the activity window or a search widget that you can insert in your layout. Both the search dialog and the widget can deliver the user’s search query to a specific activity in your application. This way, the user can initiate a search from any activity where the search dialog or widget is available, and the system starts the appropriate activity to perform the search and present results.

Using the Search Widget

  • The SearchView widget is available in Android 3.0 and higher
  • If you’re developing your application for Android 3.0 and have decided to use the search widget, we recommend that you insert the search widget as an action view in the app bar, instead of using the search dialog

Configuring the search widget

activity_toolbar_search.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.compindia.materialthemeapp.ToolbarSearchActivity"
>

<android.support.v7.widget.Toolbar
android:id="@+id/tb_toolbarsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
/>
<ListView
android:id="@+id/lv_toolbarsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tb_toolbarsearch"
></ListView>
</RelativeLayout>

ToolbarSearchActivity.java

public class ToolbarSearchActivity extends AppCompatActivity  implements SearchView.OnQueryTextListener{

private Toolbar tbMainSearch;
private ListView lvToolbarSerch;
private String TAG = ToolbarSearchActivity.class.getSimpleName();
String[] arrays = new String[]{"98411", "98422", "98433", "98444", "98455"};
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar_search);
setUpViews();
}

private void setUpViews() {
tbMainSearch = (Toolbar)findViewById(R.id.tb_toolbarsearch);
lvToolbarSerch =(ListView) findViewById(R.id.lv_toolbarsearch);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrays);
lvToolbarSerch.setAdapter(adapter);
setSupportActionBar(tbMainSearch);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem mSearchmenuItem = menu.findItem(R.id.menu_toolbarsearch);
SearchView searchView = (SearchView) mSearchmenuItem.getActionView();
searchView.setQueryHint("enter Text");
searchView.setOnQueryTextListener(this );
Log.d(TAG, "onCreateOptionsMenu: mSearchmenuItem->" + mSearchmenuItem.getActionView());
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
Log.d(TAG, "onQueryTextSubmit: query->"+query);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
Log.d(TAG, "onQueryTextChange: newText->" + newText);
adapter.getFilter().filter(newText);
return true;
}
}

menu_search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/menu_toolbarsearch"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>