Wednesday 17 August 2016

SearchView


A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.
Android  provide search dialog and search widget to handle search query of user..
Both provide the same search features, but in slightly different ways:
  • The search dialog is a UI component appears at the top of the activity.
The dialog can also provide search suggestions while the user types.
  • The search widget is an instance of SearchView that you can place anywhere in your layout.
Note : By default, the search widget behaves like a standard EditText widget and doesn't do anything, but you can configure it so that the Android system handles all input events, delivers queries to the appropriate activity, and provides search suggestions (just like the search dialog).
Other features available for the search dialog and widget include:
  • Voice search
  • Search suggestions based on recent queries
  • Search suggestions that match actual results in your application data
https://developer.android.com/images/search/search-ui.png

Add the Search View to the App Bar


To add a SearchView widget to the app bar, create a file named res/menu/options_menu.xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item. The collapseActionView attribute allows your SearchView to expand to take up the whole app bar and collapse back down into a normal app bar item when not in use. Because of the limited app bar space on handset devices, using the collapsibleActionView attribute is recommended to provide a better user experience.
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android2="http://schemas.android.com/apk/res/android"

    xmlns:android="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/search"

        android:title="@string/search_title"

        android:icon="@drawable/ic_search"

        android:showAsAction="collapseActionView|ifRoom"

        android:actionViewClass="android.widget.SearchView"

        android2:title="Search" />

</menu>
Note: If you already have an existing XML file for your menu items, you can add the <item> element to that file instead.
To display the SearchView in the app bar, inflate the XML menu resource (res/menu/options_menu.xml) in the onCreateOptionsMenu() method of your activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    return true;
}
If you run your app now, the SearchView appears in your app's app bar, but it isn't functional. You now need to define how the SearchView behaves.







































22

No comments:

Post a Comment