@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    super.onCreateOptionsMenu(menu);

    final MenuItem searchItem = menu.findItem(R.id.menu_search);
    searchItem.setShowAsAction(
        MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String s) {
            searchItem.collapseActionView();
            // setCurrentQueryString(s);
            Intent intent = new Intent(MainActivity.this, SearchActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("searchText", s);
            intent.putExtras(bundle);
            startActivity(intent);
            return true;
          }

          @Override
          public boolean onQueryTextChange(String s) {
            Log.e("onQueryTextChange", s);
            return false;
          }
        });
    View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    if (searchPlate != null) {
      searchPlate.setBackgroundResource(R.color.white);
      TextView searchText =
          (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
      if (searchText != null) {
        searchText.setHintTextColor(Color.GRAY);
        searchText.setTextColor(Color.BLACK);
      }
    }
    return true;
  }
 private void moveSearchIconFromBeginToEnd(SearchView searchView) {
   ImageView searchIcon =
       (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
   ViewGroup parent = (ViewGroup) searchIcon.getParent();
   parent.removeView(searchIcon);
   parent.addView(searchIcon);
 }
  private void initActionView(MenuItem searchItem) {
    if (searchView != null) return;
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    try { // 更改搜索按钮的icon
      int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
      ImageView v = (ImageView) searchView.findViewById(searchImgId);
      v.setImageResource(R.drawable.ic_menu_search);
    } catch (Exception e) {
      Global.errorLog(e);
    }
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String s) {
            return true;
          }

          @Override
          public boolean onQueryTextChange(String s) {
            ++sSearchPos;
            searchAdapter.reload(searchView.getQuery().toString());
            return true;
          }
        });
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contactlist);

    mAppController = AppController.getInstance();
    mContext = this;

    tvHinweis = (TextView) findViewById(R.id.tvHinweis);

    Toolbar toolbar = (Toolbar) findViewById(R.id.activity_contacts_toolbar);
    if (toolbar != null) {
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
      // getSupportActionBar().setSubtitle(null);
    }

    suggestions =
        new SearchRecentSuggestions(
            this, OrderSuggestionProvider.AUTHORITY, OrderSuggestionProvider.MODE);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);
    searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH); // Lupe anzeigen in der Tastatur
    // searchView.setSubmitButtonEnabled(true); //OK-Button anzeigen
    SearchView.SearchAutoComplete searchAutoComplete =
        (SearchView.SearchAutoComplete)
            searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

    // Collapse the search menu when the user hits the back key
    searchAutoComplete.setOnFocusChangeListener(
        new View.OnFocusChangeListener() {

          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) showSearch(false);
          }
        });

    try {
      // This sets the cursor resource ID to 0 or @null which will make it visible on white
      // background
      Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
      mCursorDrawableRes.setAccessible(true);
      mCursorDrawableRes.set(searchAutoComplete, 0);

    } catch (Exception e) {
    }

    checkServerConnection checkServerConnection = new checkServerConnection();
    checkServerConnection.execute("qw1");
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu, menu);

    SearchView searchView =
        (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_pesquisar));
    searchView.setQueryHint(this.getString(R.string.pesquisar));

    ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
        .setHintTextColor(getResources().getColor(R.color.branco));
    searchView.setOnQueryTextListener(onQuerySearchView);
  }
Exemple #6
0
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.busqueda, menu);

    MenuItem searchItem = menu.findItem(R.id.menu_search);
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setQueryHint(getString(R.string.search_hint));
    mSearchView.setOnQueryTextListener(this);
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setIconified(false);
    SearchView.SearchAutoComplete searchAutoComplete =
        (SearchView.SearchAutoComplete)
            mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(getResources().getColor(R.color.white_alpha));

    return true;
  }
  private void _defineSearchView(Menu menu) {
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchItem = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    // Theme the SearchView's AutoCompleteTextView drop down. For some reason this wasn't working in
    // styles.xml
    SearchView.SearchAutoComplete autoCompleteTextView =
        (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

    if (autoCompleteTextView != null) {
      autoCompleteTextView.setDropDownBackgroundResource(android.R.color.white);
      // set Enable Spelling Suggestions
      autoCompleteTextView.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
      int color = Color.parseColor("#ffffffff");
      Drawable drawable = autoCompleteTextView.getDropDownBackground();
      drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);

      autoCompleteTextView.setDropDownBackgroundDrawable(drawable);
      autoCompleteTextView.setTextColor(
          getResources().getColor(R.color.auto_complete_text_view_text_color));
    }

    MenuItemCompat.setOnActionExpandListener(
        searchItem,
        new MenuItemCompat.OnActionExpandListener() {

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            Log.d(TAG, "onMenuItemActionCollapse");
            return true;
          }

          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            Log.d(TAG, "onMenuItemActionExpand");
            return true;
          }
        });
    searchView.setOnSuggestionListener(
        new SearchView.OnSuggestionListener() {
          @Override
          public boolean onSuggestionSelect(int position) {
            Log.d(TAG, "onSuggestionSelect:" + position);
            return false;
          }

          @Override
          public boolean onSuggestionClick(int position) {
            Log.d(TAG, "onSuggestionClick:" + position);
            try {
              CursorAdapter c = searchView.getSuggestionsAdapter();
              if (c != null) {
                Cursor cur = c.getCursor();
                cur.moveToPosition(position);

                String cardID = cur.getString(cur.getColumnIndex(BaseColumns._ID));
                Log.d(TAG, "cardID:" + cardID);
                String query =
                    cur.getString(cur.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
                Log.d(TAG, "query:" + query);
                int insertSuggesstionResults = learnApiImplements._insertSuggesstion(cardID);
                Log.d(
                    TAG,
                    "insertSuggesstionResults "
                        + ((insertSuggesstionResults == -1) ? " OK" : " Fails"));
                _displayCard(cardID);

                // call back actionbar
                searchItem.collapseActionView();
              } else {
                Log.d(TAG, "NUll searchView.getSuggestionsAdapter()");
              }
            } catch (Exception e) {
              LazzyBeeShare.showErrorOccurred(context, "_defineSearchView", e);
            }
            return true;
          }
        });
  }