Java Code Examples for androidx.appcompat.widget.SearchView#findViewById()

The following examples show how to use androidx.appcompat.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: 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.menu_search, menu);
    MenuItem menuItem = menu.findItem(R.id.item_search);
    //得到SearchView
    mSearchView = (SearchView)menuItem.getActionView();
    mSearchAutoComplete = mSearchView.findViewById(R.id.search_src_text);
    mSearchView.setMaxWidth(R.dimen.dp_400);//设置最大宽度
    mSearchView.setSubmitButtonEnabled(true);//设置是否显示搜索框展开时的提交按钮
    mSearchView.setQueryHint(getResources().getString(R.string.searchActivity_hint));//设置输入框提示语
    mSearchView.onActionViewExpanded();//设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
    mSearchAutoComplete.setTextColor(getResources().getColor(R.color.white));//设置内容文字颜色
    //搜索框文字变化监听,搜索按钮监听
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            mPresenter.addHistoryRecord(query);
            mPresenter.loadSearchResquest(query, 0);
            isLoadMore = false;
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            mPresenter.clearAllSearchKey(newText);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
 
Example 2
Source File: AppCompat.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static void useSimpleStyleForSearchView(SearchView searchView, String hint){
    AppCompatImageView search = searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
    search.setImageDrawable(null);
    LinearLayout plate = searchView.findViewById(R.id.search_plate);
    plate.setBackground(null);
    AppCompatImageView close = searchView.findViewById(R.id.search_close_btn);
    AppCompat.setTint(close, searchView.getResources().getColor(R.color.colorBarText));
    SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(R.id.search_src_text);
    searchAutoComplete.setHint(hint);
}
 
Example 3
Source File: BookShelfSearchView.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
private void initView(Context context) {
    LayoutInflater.from(context).inflate(R.layout.view_search_bookshelf, this, true);
    ButterKnife.bind(this);
    AppCompat.setToolbarNavIconTint(toolbar, getResources().getColor(R.color.colorBarText));
    toolbar.inflateMenu(R.menu.menu_search_view);
    MenuItem search = toolbar.getMenu().findItem(R.id.action_search);
    searchView = (SearchView) search.getActionView();
    AppCompat.useCustomIconForSearchView(searchView, getResources().getString(R.string.searchShelfBook));
    searchAutoComplete = searchView.findViewById(R.id.search_src_text);
    searchView.setMaxWidth(getResources().getDisplayMetrics().widthPixels);
    searchView.onActionViewExpanded();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            queryBooks(newText);
            return false;
        }
    });
    rvList.setLayoutManager(new LinearLayoutManager(context));
    adapter = new BookShelfListAdapter(context, -1, 1);
    rvList.setAdapter(adapter);

    toolbar.setNavigationOnClickListener(v -> {
        ViewParent parent = BookShelfSearchView.this.getParent();
        if (parent instanceof DrawerLayout) {
            ((DrawerLayout) parent).closeDrawers();
        }
    });
}
 
Example 4
Source File: AddTokensActivity.java    From SSForms with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.clear();
    getMenuInflater().inflate(R.menu.tokens_picker_menu_main, menu);
    //super.onCreateOptionsMenu(menu, getMenuInflater());

    MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
    EditText searchEditText = searchView.findViewById(R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.whiteColor));
    searchEditText.setHintTextColor(getResources().getColor(R.color.whiteColor));

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

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                simpleSearchAdapter.filter("");
            } else {
                simpleSearchAdapter.filter(newText);
            }
            return true;
        }
    });
    return true;
}
 
Example 5
Source File: AppCompat.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
public static void useCustomIconForSearchView(SearchView searchView, String hint, boolean showSearchHintIcon, boolean showBg) {
    final int normalColor = searchView.getResources().getColor(R.color.colorBarText);
    AppCompatImageView search = searchView.findViewById(androidx.appcompat.R.id.search_button);
    search.setImageResource(R.drawable.ic_search_large_black_24dp);
    setTint(search, normalColor);

    SearchView.SearchAutoComplete searchText = searchView.findViewById(R.id.search_src_text);
    searchText.setTextSize(14f);
    searchText.setPaddingRelative(searchText.getPaddingLeft(), 0, 0, 0);

    final int textSize = Math.round(searchText.getTextSize() * DRAWABLE_SCALE);
    Drawable searchIcon = searchText.getResources().getDrawable(R.drawable.ic_search_black_24dp);
    searchIcon.setBounds(0, 0, textSize, textSize);
    setTint(searchIcon, normalColor);
    searchText.setCompoundDrawablesRelative(searchIcon, null, null, null);
    searchText.setCompoundDrawablePadding(DensityUtil.dp2px(searchText.getContext(), 5));
    searchText.setIncludeFontPadding(false);

    AppCompatImageView close = searchView.findViewById(R.id.search_close_btn);
    close.setImageResource(R.drawable.ic_close_black_24dp);
    setTint(close, normalColor);

    LinearLayout plate = searchView.findViewById(R.id.search_plate);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) plate.getLayoutParams();
    params.topMargin = DensityUtil.dp2px(plate.getContext(), 6);
    params.bottomMargin = params.topMargin;
    plate.setLayoutParams(params);

    View editFrame = searchView.findViewById(R.id.search_edit_frame);
    params = (LinearLayout.LayoutParams) editFrame.getLayoutParams();
    params.leftMargin = DensityUtil.dp2px(editFrame.getContext(), 4);
    editFrame.setLayoutParams(params);

    int padding = DensityUtil.dp2px(plate.getContext(), 6);
    plate.setPaddingRelative(padding, 0, padding, 0);

    if (showBg) {
        Drawable bag = searchView.getResources().getDrawable(R.drawable.bg_search_field);
        androidx.core.view.ViewCompat.setBackground(plate, bag);
    } else {
        androidx.core.view.ViewCompat.setBackground(plate, null);
    }

    setQueryHintForSearchText(searchText, hint, showSearchHintIcon);
}