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

The following examples show how to use android.support.v7.widget.SearchView#findViewById() . 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: UIHelper.java    From uPods-android with Apache License 2.0 6 votes vote down vote up
public static void setSearchViewStyle(SearchView searchView) {
    if (searchView.findViewById(android.support.v7.appcompat.R.id.search_plate) != null) {
        searchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground(null);
    }
    if (searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text) != null) {
        EditText searchPlate = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchPlate.setHint(R.string.search_hint);
        try {
            Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
            mCursorDrawableRes.setAccessible(true);
            mCursorDrawableRes.set(searchPlate, R.drawable.white_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
        } catch (Exception e) {
        }
    }

    searchView.setBackgroundColor(Color.TRANSPARENT);
}
 
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: SearchActivity.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu_setting, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_search);
    //获取搜索框
    mSearchView = (SearchView) menuItem.getActionView();
    //设置搜索hint
    mSearchView.setQueryHint(getString(R.string.search_keyword));
    mSearchView.onActionViewExpanded();
    //去除搜索框背景
    deleteSearchPlate();
    searchAutoComplete =  mSearchView.findViewById(R.id.search_src_text);
    searchAutoComplete.setHintTextColor(ContextCompat.getColor(this, R.color._60ffffff));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ImageView mCloseView =  mSearchView.findViewById(R.id.search_close_btn);
        mCloseView.setBackground(ContextCompat.getDrawable(this, R.drawable.ripple_close));
    }

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            keyword = query;
            refreshData();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                keyword = newText;
                if (mHotwordDatas.size() == 0)
                    loadTagDatas();
            }

            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
 
Example 4
Source File: SearchActivity.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView =
            (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);
    searchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
    searchView.setQuery(mPresenter.getSearchModels().get(0).getQuery(), false);
    if (isInputMode) {
        MenuItemCompat.expandActionView(searchItem);
    } else {
        MenuItemCompat.collapseActionView(searchItem);
    }
    MenuItemCompat.setOnActionExpandListener(searchItem, this);

    AutoCompleteTextView autoCompleteTextView = searchView
            .findViewById(android.support.v7.appcompat.R.id.search_src_text);
    autoCompleteTextView.setThreshold(0);
    autoCompleteTextView.setAdapter(new ArrayAdapter<>(this,
            R.layout.layout_item_simple_list, mPresenter.getSearchRecordList()));
    autoCompleteTextView.setDropDownBackgroundDrawable(new ColorDrawable(ViewUtils.getWindowBackground(getActivity())));
    autoCompleteTextView.setOnItemClickListener((parent, view, position, id) -> {
        onQueryTextSubmit(parent.getAdapter().getItem(position).toString());
    });

    return super.onCreateOptionsMenu(menu);
}
 
Example 5
Source File: FriendSearchActivity.java    From CoolChat with Apache License 2.0 5 votes vote down vote up
private void initViews() {
    layout_points = (LinearLayout) findViewById(R.id.layout_points);
    SearchView searchView = (SearchView) findViewById(R.id.search_view);
    //设置显示提交按钮
    searchView.setSubmitButtonEnabled(true);
    //一开始就处于显示SearchView的状态
    searchView.setIconifiedByDefault(false);
    //设置一开始就显示输入框
    searchView.setIconified(true);
    searchView.clearFocus();
    SearchView.SearchAutoComplete editText = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
    editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchFriend(query.trim());
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (!newText.isEmpty()) {
                searchFriend(newText);
            }
            return false;
        }
    });
}
 
Example 6
Source File: CityActivity.java    From easyweather with MIT License 5 votes vote down vote up
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_city_list,menu);
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        final MenuItem searchItem = menu.findItem(R.id.search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        if(searchView == null) return false;
//        searchView.setIconified(false);
        searchView.setQueryHint("搜索城市,暂不支持拼音");
        searchView.setSearchableInfo( searchManager.getSearchableInfo(CityActivity.this.getComponentName()) );
        AutoCompleteTextView textView = (AutoCompleteTextView)searchView.findViewById(R.id.search_src_text);
        if (textView != null){
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        }

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                }
                searchView.clearFocus();

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return true;
    }
 
Example 7
Source File: SearchViewUtils.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
public static void setSearchIcon(@NonNull final SearchView searchView,
        @DrawableRes final int icon) {
    View iconView = searchView
            .findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    if (iconView instanceof ImageView) {
        ((ImageView) iconView).setImageResource(icon);
    }

    iconView = searchView
            .findViewById(android.support.v7.appcompat.R.id.search_button);
    if (iconView instanceof ImageView) {
        ((ImageView) iconView).setImageResource(icon);
    }
}
 
Example 8
Source File: SearchViewUtil.java    From MVPAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
     * Sets the searchview's hint icon and text.
     *
     * @param searchView
     * @param drawableResource
     * @param hintText
     */
    public static void setSearchHintIcon(SearchView searchView, int drawableResource, String hintText) {
        try {
            // Accessing the SearchAutoComplete
            int queryTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            View autoComplete = searchView.findViewById(queryTextViewId);

            //Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");

            TextView searchBox = (TextView) searchView.findViewById(R.id.search_src_text);


            SpannableStringBuilder stopHint = new SpannableStringBuilder("   ");
            stopHint.append(hintText);

// Add the icon as an spannable
            Drawable searchIcon = searchView.getContext().getResources().getDrawable(drawableResource);
            Float rawTextSize = searchBox.getTextSize();
            int textSize = (int) (rawTextSize * 1.25);
            searchIcon.setBounds(0, 0, textSize, textSize);
            stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            // Set the new hint text
            searchBox.setHint(stopHint);
            //searchBox.setTextColor(Color.WHITE);
            searchBox.setHintTextColor(Color.LTGRAY);

        } catch (Exception e) {
            Log.e("SearchView", e.getMessage(), e);
        }
    }
 
Example 9
Source File: SearchBook.java    From iSCAU-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    getMenuInflater().inflate(R.menu.menu_searchbook, menu);
    menuItemSearch = menu.findItem(R.id.search_button);
    searchView = (SearchView) MenuItemCompat.getActionView(menuItemSearch);
    searchView.setQueryHint(getString(R.string.hint_searchbook));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(oQueryTextListener);
    ImageView mSearchHintIcon = (ImageView) searchView.findViewById(R.id.search_mag_icon);
    mSearchHintIcon.setVisibility(View.GONE);
    MenuItemCompat.expandActionView(menuItemSearch);
    return true;
}
 
Example 10
Source File: SearchViewUtil.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
     * Sets the searchview's hint icon and text.
     *
     * @param searchView
     * @param drawableResource
     * @param hintText
     */
    public static void setSearchHintIcon(SearchView searchView, int drawableResource, String hintText) {
        try {
            // Accessing the SearchAutoComplete
            int queryTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            View autoComplete = searchView.findViewById(queryTextViewId);

            //Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");

            TextView searchBox = (TextView) searchView.findViewById(R.id.search_src_text);


            SpannableStringBuilder stopHint = new SpannableStringBuilder("   ");
            stopHint.append(hintText);

// Add the icon as an spannable
            Drawable searchIcon = searchView.getContext().getResources().getDrawable(drawableResource);
            Float rawTextSize = searchBox.getTextSize();
            int textSize = (int) (rawTextSize * 1.25);
            searchIcon.setBounds(0, 0, textSize, textSize);
            stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            // Set the new hint text
            searchBox.setHint(stopHint);
            //searchBox.setTextColor(Color.WHITE);
            searchBox.setHintTextColor(Color.LTGRAY);

        } catch (Exception e) {
            Log.e("SearchView", e.getMessage(), e);
        }
    }
 
Example 11
Source File: GlobalSearchFragment.java    From Android-Remote with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();

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

    // Create a listener for search change
    final MenuItem search = menu.findItem(R.id.global_search_menu_search);
    final SearchView searchView = (SearchView) search.getActionView();
    searchView.setIconifiedByDefault(true);
    searchView.setIconified(false);

    final SearchView.OnQueryTextListener queryTextListener
            = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            mSwipeRefreshLayout.setRefreshing(true);
            mEmptyView.setRefreshing(true);

            Message msg = Message.obtain();

            msg.obj = ClementineMessageFactory.buildGlobalSearch(query);
            App.ClementineConnection.mHandler.sendMessage(msg);

            hideSoftInput();

            // Set the actionbar title
            mActionBar.setTitle(getResources().getString(R.string.global_search_query, query));
            mActionBar.setSubtitle("/");

            // Query must be empty in order to collapse the search view.
            searchView.setQuery("", false);
            searchView.setIconified(true);

            // Remove currently present adapters
            mAdapters.clear();
            showList();

            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
    searchView.setQueryHint(getString(R.string.global_search_search));

    EditText searchText = (EditText) searchView.findViewById(
            android.support.v7.appcompat.R.id.search_src_text);
    searchText.setHintTextColor(ContextCompat.getColor(getActivity(),
            R.color.searchview_edittext_hint));

    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 12
Source File: LibraryFragment.java    From Android-Remote with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPrepareOptionsMenu(Menu menu) {
    // Create a listener for search change
    SearchView searchView = (SearchView) menu.findItem(
            R.id.library_menu_search).getActionView();

    final SearchView.OnQueryTextListener queryTextListener
            = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // Set the filter text as the fragments might not yet
            // created. Only the left and right fragment from the
            // currently active is created (onCreate() called).
            // Therefore the other adapters are not yet created,
            // onCreate filters for this string given in setFilterText()
            if (!mAdapters.isEmpty()) {
                mAdapters.getLast().getFilter().filter(newText);
                mLastFilter = newText;

                mLibraryEmptyText.setText(R.string.library_no_search_results);
            }
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // Do something
            if (!mAdapters.isEmpty()) {
                mAdapters.getLast().getFilter().filter(query);
                mLastFilter = query;
            }

            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
    searchView.setQueryHint(getString(R.string.playlist_search_hint));

    EditText searchText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchText.setHintTextColor(ContextCompat.getColor(getActivity(),
            R.color.searchview_edittext_hint));

    super.onPrepareOptionsMenu(menu);
}
 
Example 13
Source File: PlaylistFragment.java    From Android-Remote with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPrepareOptionsMenu(Menu menu) {
    // Create a listener for search change
    SearchView searchView = (SearchView) menu.findItem(R.id.playlist_menu_search)
            .getActionView();

    final SearchView.OnQueryTextListener queryTextListener
            = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // Set the filter text as the fragments might not yet
            // created. Only the left and right fragment from the
            // currently active is created (onCreate() called).
            // Therefore the other adapters are not yet created,
            // onCreate filters for this string given in setFilterText()
            setFilterText(newText);
            if (getAdapter() != null) {
                getAdapter().getFilter().filter(newText);
            }
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // Do something
            setFilterText(query);
            if (getAdapter() != null) {
                getAdapter().getFilter().filter(query);
            }

            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
    searchView.setQueryHint(getString(R.string.playlist_search_hint));

    EditText searchText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchText.setHintTextColor(ContextCompat.getColor(getActivity(),
            R.color.searchview_edittext_hint));

    super.onPrepareOptionsMenu(menu);
}
 
Example 14
Source File: SearchFragment.java    From Bangumi-Android with MIT License 4 votes vote down vote up
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Toolbar toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
    toolbar.setTitle(null);
    mActivity.setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_action_arrow);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mActivity.getSupportFragmentManager().popBackStack();
        }
    });

    // setup search view
    final SearchView search = (SearchView) getView().findViewById(R.id.search);

    ImageView searchIcon = (ImageView) search.findViewById(R.id.search_mag_icon);
    searchIcon.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            search.clearFocus();
            if (query.trim().length() > 0) {
                searchSubjects(query);
            }
            return false;
        }

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

    // setup recyclerview
    adapter = new CardRecyclerAdapter(getActivity(), data);
    RecyclerView mRecyclerView = (RecyclerView) getView().findViewById(R.id.recycler_wrapper).findViewById(R.id.recycler);
    mRecyclerView.addItemDecoration(new MarginDecoration(getActivity()));
    mRecyclerView.setAdapter(adapter);

    mSwipeRefresh = (SwipeRefreshLayout) getView().findViewById(R.id.recycler_wrapper);
    mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mSwipeRefresh.setRefreshing(false);
        }
    });
}
 
Example 15
Source File: XposedAppManagerActivity.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            getMenuInflater().inflate(R.menu.white_list_activity_menu, menu);
            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            final SearchView searchView = (SearchView) menu.findItem(R.id.ab_search).getActionView();

            selectAll=menu.findItem(R.id.select_all);
            setSelection=menu.findItem(R.id.setSelection);

            final SearchView.SearchAutoComplete searchEditText = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

            searchEditText.setTextColor(getResources().getColor(R.color.white));
            searchView.setQueryHint("Search");

            // 将搜索按钮放到搜索输入框的外边
            searchView.setIconifiedByDefault(false);


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

                @Override
                public boolean onQueryTextChange(String newText) {
                    refreshListByQuery(searchView.getQuery().toString());
                    return true;
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    refreshListByQuery("");
                    return false;
                }
            });

//            MenuItem menuItem=menu.add("恢复默认");
//            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
//            menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//                @Override
//                public boolean onMenuItemClick(MenuItem item) {
//                    if(mSelectedApplicationInfos==null){
//                        SnackBarUtil.show(toolbar,R.string.wait_until_loaded);
//                        return true;
//                    }
//                    for (AppListAdapter.ApplicationInfoWrap app:mSelectedApplicationInfos){
//                        app.isSelected=false;
//                    }
//                    mSelectedApplicationInfos.clear();
//                    mAppAdapter.notifyDataSetChanged();
//                    refreshTV();
//                    return true;
//                }
//            });
        }
        setSelection.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                showPopupMenu(toolbar);
                return true;
            }
        });
        refreshMenu(false);
        return true;
    }
 
Example 16
Source File: WhiteListActivity.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            getMenuInflater().inflate(R.menu.white_list_activity_menu, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            final SearchView searchView = (SearchView) menu.findItem(R.id.ab_search).getActionView();

            selectAll = menu.findItem(R.id.select_all);
            setSelection = menu.findItem(R.id.setSelection);

            final SearchView.SearchAutoComplete searchEditText = searchView.findViewById(R.id.search_src_text);

            searchEditText.setTextColor(getResources().getColor(R.color.white));
            searchView.setQueryHint("Search");

            // 将搜索按钮放到搜索输入框的外边
            searchView.setIconifiedByDefault(false);


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

                @Override
                public boolean onQueryTextChange(String newText) {
                    refreshListByQuery(searchView.getQuery().toString());
                    return true;
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    refreshListByQuery("");
                    return false;
                }
            });

//            MenuItem menuItem=menu.add("恢复默认");
//            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
//            menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//                @Override
//                public boolean onMenuItemClick(MenuItem item) {
//                    if(mSelectedApplicationInfos==null){
//                        SnackBarUtil.show(toolbar,R.string.wait_until_loaded);
//                        return true;
//                    }
//                    for (AppListAdapter.ApplicationInfoWrap app:mSelectedApplicationInfos){
//                        app.isSelected=false;
//                    }
//                    mSelectedApplicationInfos.clear();
//                    mAppAdapter.notifyDataSetChanged();
//                    refreshTV();
//                    return true;
//                }
//            });
        }
        setSelection.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                showPopupMenu(toolbar);
                return true;
            }
        });
        refreshMenu(false);
        return true;
    }
 
Example 17
Source File: FloatViewWhiteListActivity.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        getMenuInflater().inflate(R.menu.white_list_activity_menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) menu.findItem(R.id.ab_search).getActionView();

        selectAll = menu.findItem(R.id.select_all);
        setSelection = menu.findItem(R.id.setSelection);

        final SearchView.SearchAutoComplete searchEditText = searchView.findViewById(R.id.search_src_text);

        searchEditText.setTextColor(getResources().getColor(R.color.white));
        searchView.setQueryHint("Search");

        // 将搜索按钮放到搜索输入框的外边
        searchView.setIconifiedByDefault(false);


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

            @Override
            public boolean onQueryTextChange(String newText) {
                refreshListByQuery(searchView.getQuery().toString());
                return true;
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                refreshListByQuery("");
                return false;
            }
        });

    }
    refreshMenu(false);
    return true;
}
 
Example 18
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;

}