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:mroza.forms.ChooseProgramActivity.java

private void clearSearchField() {
    if (menu != null) {
        android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) menu
                .findItem(R.id.action_search).getActionView();
        searchView.setQuery("", false);
        MenuItemCompat.collapseActionView(menu.findItem(R.id.action_search));
    }/*  w w  w .j  av a 2s  .com*/

    searchQuery = null;
}

From source file:in.rab.ordboken.Ordboken.java

public SearchView initSearchView(Activity activity, Menu menu, String query, Boolean focus) {
    SearchManager searchManager = (SearchManager) activity.getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) activity.findViewById(R.id.mySearchView);

    searchView.setSearchableInfo(//w  w  w .  ja v a 2 s.c  om
            searchManager.getSearchableInfo(new ComponentName(activity, MainActivity.class)));

    // Hack to get the magnifying glass icon inside the EditText
    searchView.setIconifiedByDefault(true);
    searchView.setIconified(false);

    // Hack to get rid of the collapse button
    searchView.onActionViewExpanded();

    if (!focus) {
        searchView.clearFocus();
    }

    // searchView.setSubmitButtonEnabled(true);
    searchView.setQueryRefinementEnabled(true);

    if (query != null) {
        searchView.setQuery(query, false);
    }

    return searchView;
}

From source file:org.bwgz.quotation.activity.SearchResultsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.d(TAG, String.format("onCreateOptionsMenu - menu: %s", menu));
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    Intent intent = getIntent();//from www  . ja  va 2s  . co  m
    if (intent != null) {
        int type = intent.getIntExtra(EXTRA_SEARCH_TYPE, FreebaseSearch.SEARCH_TYPE_KEYWORD);
        Log.d(TAG, String.format("onCreateOptionsMenu - type: %s", type));
        setSearchHint(menu, type);

        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.d(TAG, String.format("onCreateOptionsMenu - query: %s", query));
        if (query != null) {
            searchView.setIconified(true);
            searchView.setQuery(query, false);
            searchView.clearFocus();
        }
    }

    return true;
}

From source file:de.tap.easy_xkcd.Activities.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchMenuItem = menu.findItem(R.id.action_search);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override//from ww w  . ja v  a2 s .  com
        public boolean onQueryTextSubmit(String query) {
            MenuItem searchMenuItem = getSearchMenuItem();
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);
            //Hide Keyboard
            View view = MainActivity.this.getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            View view = getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(view, 0);
            }
            searchView.requestFocus();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            View view = getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
            return true;
        }
    });
    if (prefHelper.hideDonate())
        menu.findItem(R.id.action_donate).setVisible(false);
    return true;
}

From source file:de.enlightened.peris.PerisMain.java

@Override
public final boolean onCreateOptionsMenu(final Menu menu) {
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if (this.serverUserid == null || !getString(R.string.subforum_id).contentEquals("0")) {
        searchView.setVisibility(View.GONE);
    } else {/*  ww  w .j a  v a2s .  c o  m*/
        if (ThemeSetter.getForegroundDark(this.background)) {
            searchMenuItem.setIcon(R.drawable.ic_action_search_dark);
        }
    }

    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(final View view, final boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        public boolean onQueryTextChange(final String newText) {
            // TODO Auto-generated method stub
            return false;
        }

        @SuppressWarnings("checkstyle:requirethis")
        public boolean onQueryTextSubmit(final String query) {
            if (getActionBar() != null) {
                getActionBar().setSubtitle(baseSubtitle);
            }
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);

            final Bundle bundle = new Bundle();
            bundle.putString("subforum_name", (String) "Search - " + query);
            bundle.putString("subforum_id", (String) "search");
            bundle.putString("query", (String) query);
            bundle.putString("background", (String) background);
            bundle.putString("icon", (String) "n/a");
            bundle.putString("inTab", (String) "N");
            loadCategory(bundle, "SEARCH_QUERY", false);

            return false;
        }
    });
    return true;
}

From source file:com.cypress.cysmart.CommonFragments.ProfileScanningFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();/*from   w w  w.  j  av a  2 s  .c  om*/
    inflater.inflate(R.menu.global, menu);
    MenuItem item = menu.findItem(R.id.graph);
    MenuItem log = menu.findItem(R.id.log);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    item.setVisible(false);
    log.setVisible(true);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false);
    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered

            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
                // this is your adapter that will be filtered
                if (mLeDeviceListAdapter != null) {
                    searchEnabled = true;
                    searchDeviceList(query);
                }
            }
            return false;

        }
    };
    searchView.setOnQueryTextListener(textChangeListener);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:inc.bait.jubilee.ui.fragments.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from   ww w. j  a  va2  s  . c om*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.contact_list_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    if (isSearchResultView) {
        searchItem.setVisible(false);
    }
    if (ApiHelper.hasHoneycomb()) {
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                if (searchString == null && newFilter == null) {
                    return true;
                }
                if (searchString != null && searchString.equals(newFilter)) {
                    return true;
                }
                searchString = newFilter;
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (ApiHelper.hasICS()) {
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    if (!TextUtils.isEmpty(searchString)) {
                        onSelectionCleared();
                    }
                    searchString = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (searchString != null) {
            final String savedSearchTerm = searchString;

            // Expands the search menu item
            if (ApiHelper.hasICS()) {
                searchItem.expandActionView();
            }
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:edu.ucsb.cs.cs185.inspirante.searchbyranking.SearchByRankingFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_search_by_ranking, container, false);

    final SearchView searchView = (SearchView) rootView.findViewById(R.id.search_by_ranking_search_view);
    int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchEditText = (EditText) searchView.findViewById(id);
    searchEditText.setTextColor(Color.GRAY);
    searchEditText.setHintTextColor(Color.GRAY);
    searchView.setOnQueryTextListener(searchViewListener);
    searchView.setMaxWidth(Integer.MAX_VALUE);
    searchView.setOnClickListener(new View.OnClickListener() {

        @Override/*  w  ww  .j a v  a2s  .co  m*/
        public void onClick(View view) {
            searchView.setFocusable(true);
            searchView.setIconified(false);
            searchView.requestFocusFromTouch();
        }
    });

    mInspireRecyclerView = (RecyclerView) rootView.findViewById(R.id.search_rv);
    mInspireRecyclerView.setHasFixedSize(true);
    mInspireRecyclerView.setNestedScrollingEnabled(false);

    frequentTags = ItemCards.getInstance(getContext()).getFrequentTags();

    mInspireAdapter = new RankByTagAdapter(getContext(), this, frequentTags);
    mInspireAdapter.setHasStableIds(true);

    mLayoutManager = new LinearLayoutManager(getContext());
    mLayoutManager.setItemPrefetchEnabled(true);

    mInspireRecyclerView.setLayoutManager(mLayoutManager);

    mInspireAdapter.setOnItemClickListener(new RankByTagAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            TextView tv = (TextView) view;
            Intent intent = new Intent(getActivity(), TagAndImagesActivity.class);
            intent.putExtra("TAG", tv.getText());
            startActivity(intent);
        }
    });

    ItemCards itemCards = ItemCards.getInstance(getContext());
    itemCards.setAdapter(mInspireAdapter);

    mInspireRecyclerView.setAdapter(mInspireAdapter);
    mInspireAdapter.notifyDataSetChanged();

    // Set Tag recycler view
    mTagRecyclerView = (RecyclerView) rootView.findViewById(R.id.search_tags_rv);
    mTagRecyclerView.setHasFixedSize(true);
    mTagRecyclerView.setNestedScrollingEnabled(false);
    mTagsAdapter = new TagsAdapter(getContext(), frequentTags);
    mTagsAdapter.setHasStableIds(true);
    RecyclerView.LayoutManager tagsLayoutManager = new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, false);
    tagsLayoutManager.setItemPrefetchEnabled(true);

    mTagRecyclerView.setLayoutManager(tagsLayoutManager);

    mTagsAdapter.setOnItemClickListener(new TagsAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            String tag = mTagsAdapter.getTag(position);
            searchViewListener.onQueryTextChange(tag);
            //searchView.setFocus
            searchView.setQuery(tag, true);
            searchView.setIconified(false);
            searchView.clearFocus();
            mLayoutManager.scrollToPosition(0);

        }
    });

    mTagRecyclerView.setAdapter(mTagsAdapter);
    mTagsAdapter.notifyDataSetChanged();

    return rootView;
}

From source file:co.tinode.tindroid.ContactsFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.menu_contacts, menu);

    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // 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();
    // searchView.setFocusable(true);

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

    // Set listeners for SearchView
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override/*w w  w  .j av a 2 s .  c  o m*/
        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.
            getLoaderManager().restartLoader(ContactsQuery.CORE_QUERY_ID, null, mContactsLoaderCallback);
            return true;
        }
    });

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            Log.d(TAG, "EXPAND onMenuItemActionCollapse");
            searchView.setIconified(false);
            searchView.requestFocusFromTouch();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            Log.d(TAG, "COLLAPSE onMenuItemActionCollapse");
            searchView.clearFocus();
            // 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.CORE_QUERY_ID, null, mContactsLoaderCallback);
            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
        searchItem.expandActionView();

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

From source file:com.jackleeentertainment.oq.ui.layout.fragment.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//from  w ww  .j  a  va  2  s .c  om
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 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);
        }
    }
}