Java Code Examples for android.support.v7.widget.SearchView#setOnQueryTextListener()

The following examples show how to use android.support.v7.widget.SearchView#setOnQueryTextListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From AndroidSamples with Apache License 2.0 7 votes vote down vote up
/**
 * 搜索栏
 */
private void initSearchView() {
    MenuItem search = mToolbar.getMenu().findItem(R.id.menu_search);
    final SearchView searchView = (SearchView) search.getActionView();

    searchView.setQueryHint("请输入学号:");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.e(TAG, query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            Log.e(TAG, s);
            return false;
        }
    });
}
 
Example 2
Source File: MusicContainerListActivity.java    From PlayMusicExporter with MIT License 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.music_track_list, menu);

    // Finds the search item and create the search view
    MenuItem itemSearch = menu.findItem(R.id.action_search);
    mSearchView = (SearchView)MenuItemCompat.getActionView(itemSearch);

    if (mSearchView != null) {
        // Sets the search listener
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setIconifiedByDefault(true);

        // Hack to change the default placeholder
        EditText searchEditText = (EditText) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchEditText.setHint(R.string.search);
    }

    return true;
}
 
Example 3
Source File: MainActivity.java    From ActivityDiary with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.action_filter);
    searchView = (SearchView) searchMenuItem.getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setOnCloseListener(this);
    searchView.setOnQueryTextListener(this);
    // setOnSuggestionListener -> for selection of a suggestion
    // setSuggestionsAdapter
    searchView.setOnSearchClickListener(v -> setSearchMode(true));
    return true;
}
 
Example 4
Source File: MainActivity.java    From NintendoSwitchEShopHelper with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_view, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_item_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            QueryPreferences.setStoredQuery(mContext, query);
            updateItems();
            searchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            QueryPreferences.setStoredQuery(mContext, newText.equals("") ? null : newText);
            updateItems();
            return true;
        }
    });

    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            QueryPreferences.setStoredQuery(mContext, null);
            updateItems();
            searchView.onActionViewCollapsed();
            return true;
        }
    });

    return super.onCreateOptionsMenu(menu);
}
 
Example 5
Source File: MusicFragment.java    From TouchNews with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_music, menu);
    super.onCreateOptionsMenu(menu, inflater);
    mMenuItemSearch = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    mMenuItemSearch.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            mListView.setVisibility(View.GONE);
            //刷新待选List 为默认值
            updateSearchList(mSearchChooseDef);
            return false;
        }
    });
    //文本输入框可见、显示ListView
    mMenuItemSearch.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                mListView.setVisibility(View.VISIBLE);
            }
        }
    });
    mMenuItemSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //文本输入改变实时搜索关键字
            mMusicsPresenter.searchMusic(newText);
            //文本输入改变 List 滑动到顶部
            mListView.setSelection(0);
            return true;
        }
    });

}
 
Example 6
Source File: AppChoicesActivity.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.app_search, menu);

    MenuItem searchItem = menu.findItem(R.id.menu_app_search);
    MenuItem filterEnabledAppsItem = menu.findItem(R.id.menu_filter_enabled);

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getString(R.string.search_query_hint));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return recyclerViewShowSearchResult(query);
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return recyclerViewShowSearchResult(newText);
        }
    });

    searchItem.setEnabled(false);
    filterEnabledAppsItem.setEnabled(false);
    filterEnabledAppsItem.setChecked(mShowOnlyEnabledApps);

    return true;
}
 
Example 7
Source File: FileBrowserWithCustomHandler.java    From Android-FileBrowser-FilePicker with MIT License 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.toolbar_default_menu, menu);
    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchMenuItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView)mSearchMenuItem.getActionView();
    // Assumes current activity is the searchable activity
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    //searchView.setSubmitButtonEnabled(true);
    mSearchView.setOnQueryTextListener(mSearchViewListener);
    return true;
}
 
Example 8
Source File: MainActivity.java    From XposedAppLocale with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem item = (MenuItem) menu.findItem(R.id.action_search);
    mSearchView = (SearchView) MenuItemCompat.getActionView(item);
    mSearchView.setOnQueryTextListener(this);

    return true;
}
 
Example 9
Source File: AllCurrencyListFragment.java    From CryptoBuddy with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    getActivity().getMenuInflater().inflate(R.menu.all_currency_list_tab_menu, menu);
    searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);
    // Detect SearchView icon clicks
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchViewFocused = true;
            setItemsVisibility(menu, searchItem, false);
            drawerController.hideHamburger();
        }
    });
    // Detect SearchView close
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            searchViewFocused = false;
            setItemsVisibility(menu, searchItem, true);
            drawerController.showHamburger();
            return false;
        }
    });
    if (searchViewFocused) ((AppCompatActivity)mContext).getSupportActionBar().setTitle("");
    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 10
Source File: MainActivity.java    From Android-Cucumber-BDD-Sample with Apache License 2.0 5 votes vote down vote up
private void initializeViews() {
    bookAdapter = new BookAdapter();

    RecyclerView bookRecycler = (RecyclerView) findViewById(R.id.bookRecycler);
    bookRecycler.setLayoutManager(new LinearLayoutManager(this));
    bookRecycler.setAdapter(bookAdapter);

    SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(this);
}
 
Example 11
Source File: BaseFragment.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setHasSearchView(IOnSearchViewChangeListener listener,
                             Menu menu, int menu_id) {
    mOnSearchViewChangeListener = listener;
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu
            .findItem(menu_id));
    if (mSearchView != null) {
        mSearchView.setOnCloseListener(closeListener);
        mSearchView.setOnQueryTextListener(searchViewQueryListener);
        mSearchView.setIconifiedByDefault(true);
    }
}
 
Example 12
Source File: HeroListFragment.java    From tup.dota2recipe with Apache License 2.0 5 votes vote down vote up
/**
 * setup Hero Searchable SearchView
 * 
 * @param searchItem
 */
private void setupHeroSearchView(MenuItem searchItem) {
    mSearchView = (SearchView)MenuItemCompat.getActionView(searchItem);
    // TODO SearchableInfo
    if (mSearchView != null)
        mSearchView.setOnQueryTextListener(this);
}
 
Example 13
Source File: ListArticleFragment.java    From CodePolitan with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);
    searchView = (SearchView) menu.getItem(0).getActionView();
    searchView.setOnQueryTextListener(this);
}
 
Example 14
Source File: SearchMusicActivity.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search_music, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);
    searchView.onActionViewExpanded();
    searchView.setQueryHint(getString(R.string.search_tips));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchMusic(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    searchView.setSubmitButtonEnabled(true);
    try {
        Field field = searchView.getClass().getDeclaredField("mGoButton");
        field.setAccessible(true);
        ImageView mGoButton = (ImageView) field.get(searchView);
        mGoButton.setImageResource(R.drawable.ic_menu_search);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return super.onCreateOptionsMenu(menu);
}
 
Example 15
Source File: BaseFragment.java    From Android-ContactPicker with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem item = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}
 
Example 16
Source File: ContactsPickerActivity.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.contact_list_menu, menu);

    mMenuStartGroupChat = menu.findItem(R.id.action_start_chat);
    updateStartGroupChatMenu();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));

    if (mSearchView != null )
    {
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        mSearchView.setIconifiedByDefault(false);

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
        {
            public boolean onQueryTextChange(String newText)
            {
                mSearchString = newText;
                doFilterAsync(mSearchString);
                return true;
            }

            public boolean onQueryTextSubmit(String query)
            {
                mSearchString = query;
                doFilterAsync(mSearchString);

                return true;
            }
        };

        mSearchView.setOnQueryTextListener(queryTextListener);
    }



    return true;
}
 
Example 17
Source File: OrdersFragment.java    From mobikul-standalone-pos with MIT License 4 votes vote down vote up
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        final MenuItem barcodeItem = menu.findItem(R.id.menu_item_scan_barcode);
        barcodeItem.setVisible(false);
        final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchItem.setVisible(true);
        searchItem.expandActionView();
        searchView.setQueryHint("Search your order...");
        ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.colorAccent));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchOrders = new ArrayList<>();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() > 0) {
                    newText = "%" + newText + "%";
                    DataBaseController.getInstanse().getSearchOrders(getActivity(), newText, new DataBaseCallBack() {
                        @Override
                        public void onSuccess(Object responseData, String successMsg) {
                            if (!(searchOrders.toString().equalsIgnoreCase(responseData.toString()))) {
                                if (searchOrders.size() > 0) {
                                    searchOrders.clear();
                                }
                                searchOrders.addAll((List<OrderEntity>) responseData);
                                orderAdapter = new OrderAdapter(getActivity(), searchOrders);
                                binding.orderRv.setAdapter(orderAdapter);
//                                orderAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onFailure(int errorCode, String errorMsg) {

                        }
                    });
                    return true;
                } else {
                    orderAdapter = new OrderAdapter(getActivity(), orders);
                    binding.orderRv.setAdapter(orderAdapter);
                }
                return false;
            }
        });

    }
 
Example 18
Source File: HomeFragment.java    From mobikul-standalone-pos with MIT License 4 votes vote down vote up
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchItem.setVisible(true);
        searchItem.expandActionView();
        searchView.setQueryHint("Search your product...");
        ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.colorAccent));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchProduct = new ArrayList<>();

        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //perform your click operation hereif (mMainBinding.drawerLayout != null &&
                if (((MainActivity) getActivity()).mMainBinding.drawerLayout.isDrawerOpen(GravityCompat.START))
                    ((MainActivity) getActivity()).mMainBinding.drawerLayout.closeDrawer(GravityCompat.START);

            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() > 0) {
                    newText = "%" + newText + "%";
                    DataBaseController.getInstanse().getSearchData(getActivity(), newText, new DataBaseCallBack() {
                        @Override
                        public void onSuccess(Object responseData, String successMsg) {
                            if (!(searchProduct.toString().equalsIgnoreCase(responseData.toString()))) {
                                if (searchProduct.size() > 0) {
                                    searchProduct.clear();
                                }
                                searchProduct.addAll((List<Product>) responseData);
                                productAdapter = new HomePageProductAdapter(getActivity(), searchProduct);
                                binding.productRv.setAdapter(productAdapter);
                            }
                        }

                        @Override
                        public void onFailure(int errorCode, String errorMsg) {

                        }
                    });
                    return true;
                } else {
                    productAdapter = new HomePageProductAdapter(getActivity(), products);
                    binding.productRv.setAdapter(productAdapter);
                }
                return false;
            }
        });

        final MenuItem barCodeItem = menu.findItem(R.id.menu_item_scan_barcode);
        barCodeItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
//                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
//                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
//                startActivityForResult(intent, 0);
                Intent intent = new Intent(getActivity(), BarcodeCaptureActivity.class);
                getActivity().startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
                return false;
            }
        });
    }
 
Example 19
Source File: ForumRulesFragment.java    From ForPDA with GNU General Public License v3.0 4 votes vote down vote up
private void addSearchOnPageItem(Menu menu) {
    toolbar.inflateMenu(R.menu.theme_search_menu);
    MenuItem searchOnPageMenuItem = menu.findItem(R.id.action_search);
    searchOnPageMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
    /*MenuItemCompat.setOnActionExpandListener(searchOnPageMenuItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            toggleMessagePanelItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            return true;
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            toggleMessagePanelItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);
            return true;
        }
    });*/
    SearchView searchView = (SearchView) searchOnPageMenuItem.getActionView();
    searchView.setTag(searchViewTag);

    searchView.setOnSearchClickListener(v -> {
        if (searchView.getTag().equals(searchViewTag)) {
            ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
            if (searchClose != null)
                ((ViewGroup) searchClose.getParent()).removeView(searchClose);

            ViewGroup.LayoutParams navButtonsParams = new ViewGroup.LayoutParams(App.px48, App.px48);
            TypedValue outValue = new TypedValue();
            getContext().getTheme().resolveAttribute(android.R.attr.actionBarItemBackground, outValue, true);

            AppCompatImageButton btnNext = new AppCompatImageButton(searchView.getContext());
            btnNext.setImageDrawable(App.getVecDrawable(getContext(), R.drawable.ic_toolbar_search_next));
            btnNext.setBackgroundResource(outValue.resourceId);

            AppCompatImageButton btnPrev = new AppCompatImageButton(searchView.getContext());
            btnPrev.setImageDrawable(App.getVecDrawable(getContext(), R.drawable.ic_toolbar_search_prev));
            btnPrev.setBackgroundResource(outValue.resourceId);

            ((LinearLayout) searchView.getChildAt(0)).addView(btnPrev, navButtonsParams);
            ((LinearLayout) searchView.getChildAt(0)).addView(btnNext, navButtonsParams);

            btnNext.setOnClickListener(v1 -> findNext(true));
            btnPrev.setOnClickListener(v1 -> findNext(false));
            searchViewTag++;
        }
    });

    SearchManager searchManager = (SearchManager) getMainActivity().getSystemService(Context.SEARCH_SERVICE);
    if (null != searchManager) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getMainActivity().getComponentName()));
    }

    searchView.setIconifiedByDefault(true);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            findText(newText);
            return false;
        }
    });
}
 
Example 20
Source File: SearchActivity.java    From CrimeTalk-Reader with Apache License 2.0 2 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.activity_search, menu);

    // Modify some aspects of the SearchView
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQuery(mQuery, false);
    searchView.setIconified(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {

            return false;

        }

        @Override
        public boolean onQueryTextChange(String query) {

            SearchActivity.this.mQuery = query;

            return false;

        }

    });

    final SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)
            searchView.findViewById(R.id.search_src_text);
    searchAutoComplete.setTextColor(getResources().getColor(R.color.white));

    // Recovering from orientation change, try not to have focus
    if (mQuery != null && !mQuery.isEmpty()) {

       searchView.clearFocus();

    }

    return true;

}