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

The following examples show how to use android.support.v7.widget.SearchView#setOnSearchClickListener() . 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 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 2
Source File: SubsonicFragment.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
protected void onFinishSetupOptionsMenu(final Menu menu) {
	searchItem = menu.findItem(R.id.menu_global_search);
	if(searchItem != null) {
		searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
		SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
		SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
		if(searchableInfo == null) {
			Log.w(TAG, "Failed to get SearchableInfo");
		} else {
			searchView.setSearchableInfo(searchableInfo);
		}

		String currentQuery = getCurrentQuery();
		if(currentQuery != null) {
			searchView.setOnSearchClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					searchView.setQuery(getCurrentQuery(), false);
				}
			});
		}
	}
}
 
Example 3
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 4
Source File: TaskListFragment.java    From mvp-helpers with MIT License 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.task_list_menu, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();

    if (null != actionBar) {
        final SearchView searchView = new SearchView(actionBar.getThemedContext());

        MenuItemCompat.setActionView(item, searchView);

        searchView.setOnQueryTextListener(this);
        searchView.setIconifiedByDefault(false);

        searchView.setOnSearchClickListener(v -> newTaskButton.hide());
        searchView.setOnCloseListener(this::onClose);

        MenuItemCompat.setOnActionExpandListener(item, this);
    }

    if (isPresenterAvailable()) {
        Long count = getPresenter().getTasksCount();

        menu.findItem(R.id.action_delete_all).setEnabled(count != 0);
        menu.findItem(R.id.action_delete_all).setVisible(count != 0);
    }

    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 5
Source File: TransactionActivity.java    From budget-watch with GNU General Public License v3.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.transaction_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setOnCloseListener(new SearchView.OnCloseListener()
    {
        @Override
        public boolean onClose()
        {
            _currentlySearching = false;

            // Re-populate the transactions
            onResume();

            // false: allow the default cleanup behavior on the search view on closing.
            return false;
        }
    });

    searchView.setOnSearchClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            _currentlySearching = true;
        }
    });

    return super.onCreateOptionsMenu(menu);
}
 
Example 6
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 7
Source File: MainActivity.java    From Bookster with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("dark_theme", false))
        menu.findItem(R.id.Theme).setTitle("今夜白");
    final MenuItem searchItem = menu.findItem(R.id.toolbar_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint("搜索书籍或者作者");
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchView.setQuery(lastKeyWords, false);
        }
    });


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //处理搜索结果
            try {
                lastKeyWords = query;
                Runtime runtime = Runtime.getRuntime();
                runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
                runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
                setTitle("搜索:" + query);
                for (BaseFragment fragment : mPagerAdapter.getFragments()) {
                    fragment.startSearch(query);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
 
Example 8
Source File: AnnounceFragment.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);
    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 9
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 10
Source File: MainActivity.java    From AndroidHttpCapture with MIT License 4 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.main, menu);

//        MenuItem pageButton = menu.findItem(R.id.action_page);
//        pageButton.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                createPage();
//                return true;
//            }
//        });

        filterMenuItem = menu.findItem(R.id.action_filter);
        homeItem = menu.findItem(R.id.action_home);
        searchItem = menu.findItem(R.id.search);
//        uaMenuItem = menu.findItem(R.id.ua);
//        logMenuItem = menu.findItem(R.id.log);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) searchItem.getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setFocusable(false);
        searchView.setIconifiedByDefault(true);
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setQueryHint("请输入URL关键字...");

        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchContent(PreviewFragment.getInstance());
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                PreviewFragment.getInstance().filterItem(query);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                PreviewFragment.getInstance().filterItem(newText);
                shouldExitSearchView = newText.length() == 0;
                return false;
            }
        });
        return true;
    }