Example usage for android.widget SearchView setQuery

List of usage examples for android.widget SearchView setQuery

Introduction

In this page you can find the example usage for android.widget SearchView setQuery.

Prototype

public void setQuery(CharSequence query, boolean submit) 

Source Link

Document

Sets a query string in the text field and optionally submits the query as well.

Usage

From source file:android.com.example.contactslist.ui.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from   w  w w.  ja va 2 s .c  o m*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.chris.ni.omw.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from ww  w. j  a  v a 2s.  co  m*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:br.com.mybaby.contatos.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//from  w w w.  j a  va2  s.  c  o m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contatos_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Util.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Util.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.tavant.droid.womensecurity.fragments.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from  w ww .ja v  a 2s.c o  m*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contacts_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent
    // additional
    // searches. In Android 3.0 and later, searching is done via a
    // SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching,
    // the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar
    // SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the
                // search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has
                // changed. Updates
                // the search filter, and restarts the loader to do
                // a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same
                // as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers
                // onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is
                    // expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the
                    // current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.dsdar.thosearoundme.util.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//w  ww.j  a v  a  2s  .  c  o  m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent
    // additional
    // searches. In Android 3.0 and later, searching is done via a
    // SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching,
    // the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar
    // SearchView
    if (Util.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the
                // search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has
                // changed. Updates
                // the search filter, and restarts the loader to do
                // a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same
                // as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers
                // onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is
                    // expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the
                    // current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Util.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.dsdar.thosearoundme.util.MemberAddContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//  w ww  . j  a  v a  2 s.c o m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent
    // additional
    // searches. In Android 3.0 and later, searching is done via a
    // SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching,
    // the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar
    // SearchView
    if (Util.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the
                // search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has
                // changed. Updates
                // the search filter, and restarts the loader to do
                // a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same
                // as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers
                // onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null,
                        MemberAddContactsListFragment.this);
                return true;
            }
        });

        if (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is
                    // expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the
                    // current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null,
                            MemberAddContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Util.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.example.contactslist.ui.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//from  w  w w .j  a  va2 s. co m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                /*Uri uri; 
                        
                Cursor[] atomic = new Cursor[phoneNumbers.length];
                        
                for(int i=0; i<phoneNumbers.length; i++){
                   uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                        Uri.encode(phoneNumbers[i]));
                   atomic[i] = getActivity().getContentResolver().query(uri, ContactsQuery.PROJECTION, null, null, ContactsQuery.SORT_ORDER);
                }
                        
                Cursor extendedCursor = new MergeCursor(atomic);*/

                /* Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
                 String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER};
                 String SELECTION = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%9986395899' OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%997%'";
                        
                 Cursor people = getActivity().getContentResolver().query(uri, ContactsQuery.PROJECTION, SELECTION, null, null);
                 mAdapter.swapCursor(people);*/

                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:edu.umbc.cs.ebiquity.mithril.parserapp.contentparsers.contacts.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from   w w  w  . jav  a2s.  co m*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                if (ParserApplication.getQueryOrLoader() == ParserApplication.getContactButtonLoader()) {
                    /**
                     * If the query text changes for the contact search then the loader is restarted
                     */
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    //                       Toast.makeText(getActivity(), ParserApplication.getQueryOrLoader()
                    //                             +"and"+ParserApplication.getButtonLoader(), Toast.LENGTH_LONG).show();
                } else if (ParserApplication.getQueryOrLoader()
                        .equals(ParserApplication.getContactButtonQuery())) {
                    //                       Toast.makeText(getActivity(), ParserApplication.getQueryOrLoader(), Toast.LENGTH_LONG).show();
                    getData();
                }
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    if (ParserApplication.getQueryOrLoader() == ParserApplication.getContactButtonLoader()) {
                        /**
                         * If the query text changes for the contact search then the loader is restarted
                         */
                        getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null,
                                ContactsListFragment.this);
                        //                           Toast.makeText(getActivity(), ParserApplication.getQueryOrLoader()
                        //                                 +"and"+ParserApplication.getButtonLoader(), Toast.LENGTH_LONG).show();
                    } else if (ParserApplication.getQueryOrLoader()
                            .equals(ParserApplication.getContactButtonQuery())) {
                        //                           Toast.makeText(getActivity(), ParserApplication.getQueryOrLoader(), Toast.LENGTH_LONG).show();
                        getData();
                    }
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}