public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.search, menu);

    final android.support.v7.widget.SearchView searchView =
        (android.support.v7.widget.SearchView)
            MenuItemCompat.getActionView(menu.findItem(R.id.search));

    final MenuItem searchMenuItem = menu.findItem(R.id.search);

    searchView.setOnQueryTextFocusChangeListener(
        new View.OnFocusChangeListener() {

          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
              searchMenuItem.collapseActionView();
              searchView.setQuery("", false);
            }
          }
        });

    searchView.setOnQueryTextListener(
        new android.support.v7.widget.SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextChange(String newText) {

            textlength = newText.length();
            list.clear();

            for (int i = 0; i < str_title.length; i++) {
              if (textlength <= str_title[i].length()) {
                if (str_title[i].toLowerCase().contains(newText.toLowerCase())) {

                  ItemRecent objItem = new ItemRecent();

                  objItem.setCategoryName((str_cat_name[i]));
                  objItem.setCatId(str_cat_id[i]);
                  objItem.setCId(str_cid[i]);
                  objItem.setNewsDate(str_date[i]);
                  objItem.setNewsDescription(str_desc[i]);
                  objItem.setNewsHeading(str_title[i]);
                  objItem.setNewsImage(str_image[i]);
                  list.add(objItem);
                }
              }
            }

            setAdapterToListview();
            return false;
          }

          @Override
          public boolean onQueryTextSubmit(String query) {

            return true;
          }
        });
  }
 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
   super.onCreateOptionsMenu(menu, inflater);
   inflater.inflate(R.menu.menu_select_program, menu);
   MenuItem item = menu.findItem(R.id.action_search);
   SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
   MenuItemCompat.setOnActionExpandListener(item, this);
   searchView.setOnQueryTextListener(this);
   searchView.setOnQueryTextFocusChangeListener(this);
 }
예제 #3
0
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_manage_inserts, menu);

    final MenuItem searchItem = menu.findItem(R.id.menu_search_inserts);

    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getString(R.string.insert_query_hint));
    searchView.setOnQueryTextListener(this);

    searchView.setOnQueryTextFocusChangeListener(
        new View.OnFocusChangeListener() {
          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
              MenuItemCompat.collapseActionView(searchItem);
              searchView.setQuery("", false);
            }
          }
        });

    MenuItemCompat.setOnActionExpandListener(
        searchItem,
        new MenuItemCompat.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            adapter.refreshAdapter();
            return true;
          }
        });
  }