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

The following examples show how to use androidx.appcompat.widget.SearchView#setQuery() . 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: BaseListActivity.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (mIsMultiPane) {
        getMenuInflater().inflate(R.menu.menu_item_compact, menu);
    }
    if (isSearchable()) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem menuSearch = menu.findItem(R.id.menu_search);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) mActionViewResolver.getActionView(menuSearch);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(
                new ComponentName(this, SearchActivity.class)));
        searchView.setIconified(true);
        searchView.setQuery("", false);
    }
    return super.onCreateOptionsMenu(menu);
}
 
Example 2
Source File: FavoriteFragment.java    From materialistic with Apache License 2.0 6 votes vote down vote up
private void createSearchView(MenuItem menuSearch) {
    final SearchView searchView = (SearchView) mActionViewResolver.getActionView(menuSearch);
    searchView.setQueryHint(getString(R.string.hint_search_saved_stories));
    searchView.setSearchableInfo(((SearchManager) getActivity()
            .getSystemService(Context.SEARCH_SERVICE))
            .getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconified(!mSearchViewExpanded);
    searchView.setQuery(mFilter, false);
    searchView.setOnSearchClickListener(v -> {
        mSearchViewExpanded = true;
        v.requestFocus();
    });
    searchView.setOnCloseListener(() -> {
        // trigger a dummy empty search intent, as empty query does not get submitted
        searchView.setQuery(FavoriteActivity.EMPTY_QUERY, true);
        return false;
    });
}
 
Example 3
Source File: SearchFragment.java    From Jockey with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.activity_search, menu);

    MenuItem searchItem = menu.findItem(R.id.menu_library_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            mViewModel.setSearchQuery(newText);
            return true;
        }
    });

    searchView.setIconified(false);
    if (!StringUtils.isEmpty(mViewModel.getSearchQuery())) {
        searchView.setQuery(mViewModel.getSearchQuery(), true);
    }
}
 
Example 4
Source File: ConversationActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    showActionBar();
    //return false;
    getMenuInflater().inflate(R.menu.mobicom_basic_menu_for_normal_message, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getResources().getString(R.string.search_hint));
    if (Utils.hasICS()) {
        searchItem.collapseActionView();
    }
    searchView.setOnQueryTextListener(this);
    searchView.setSubmitButtonEnabled(true);
    searchView.setIconifiedByDefault(true);

    if (quickConversationFragment != null && !TextUtils.isEmpty(quickConversationFragment.getSearchString())) {
        searchView.setIconified(false);
        searchView.setQuery(quickConversationFragment.getSearchString(), false);
    }

    return super.onCreateOptionsMenu(menu);
}
 
Example 5
Source File: ActivityMain.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
private void updateSearch(String search) {
    if (menuSearch != null) {
        SearchView searchView = (SearchView) menuSearch.getActionView();
        if (search == null) {
            if (menuSearch.isActionViewExpanded())
                adapter.getFilter().filter(searchView.getQuery().toString());
        } else {
            menuSearch.expandActionView();
            searchView.setQuery(search, true);
        }
    }
}
 
Example 6
Source File: LttrsActivity.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
    final int currentDestination = getCurrentDestinationId();
    final boolean showSearch = MAIN_DESTINATIONS.contains(currentDestination) || currentDestination == R.id.search;

    getMenuInflater().inflate(R.menu.activity_main, menu);

    mSearchItem = menu.findItem(R.id.action_search);

    mSearchItem.setVisible(showSearch);

    if (showSearch) {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        mSearchView = (SearchView) mSearchItem.getActionView();
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        if (currentDestination == R.id.search) {
            setSearchToolbarColors();
            mSearchItem.expandActionView();
            mSearchView.setQuery(lttrsViewModel.getCurrentSearchTerm(), false);
            mSearchView.clearFocus();
        }
        mSearchItem.setOnActionExpandListener(this);
    } else {
        mSearchItem = null;
        mSearchView = null;
        resetToolbarColors();
    }

    return super.onCreateOptionsMenu(menu);
}
 
Example 7
Source File: FragmentContacts.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_contacts, menu);

    MenuItem menuSearch = menu.findItem(R.id.menu_search);
    SearchView searchView = (SearchView) menuSearch.getActionView();
    searchView.setQueryHint(getString(R.string.title_search));

    if (!TextUtils.isEmpty(searching)) {
        menuSearch.expandActionView();
        searchView.setQuery(searching, true);
    }

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            searching = newText;
            adapter.search(newText);
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            searching = query;
            adapter.search(query);
            return true;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 8
Source File: ActivityMain.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public void updateMenu(Menu menu) {
    // Search
    MenuItem menuSearch = menu.findItem(R.id.menu_search);
    final SearchView searchView = (SearchView) menuSearch.getActionView();
    if (searchView != null) {
        String pkg = getIntent().getStringExtra(EXTRA_SEARCH_PACKAGE);
        if (pkg != null) {
            Log.i(TAG, "Search pkg=" + pkg);
            menuSearch.expandActionView();
            searchView.setQuery(pkg, true);
        }
    }
}
 
Example 9
Source File: MyMusicFragment.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initialize the options menu.
 * Be sure to call {@link #setHasOptionsMenu} before.
 *
 * @param menu         The container for the custom options menu.
 * @param menuInflater The inflater to instantiate the layout.
 */
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.options_menu_my_music, menu);

    mOptionMenu = menu;

    // get tint color
    int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_text_accent);

    Drawable drawable = menu.findItem(R.id.action_search).getIcon();
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, tintColor);
    mOptionMenu.findItem(R.id.action_search).setIcon(drawable);

    mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    // Check if a search string is saved from before
    if (mSearchString != null) {
        // Expand the view
        mSearchView.setIconified(false);
        menu.findItem(R.id.action_search).expandActionView();
        // Set the query string
        mSearchView.setQuery(mSearchString, false);

        OdysseyFragment fragment = mMyMusicPagerAdapter.getRegisteredFragment(mMyMusicViewPager.getCurrentItem());
        // Notify the adapter
        fragment.applyFilter(mSearchString);
    }

    mSearchView.setOnQueryTextListener(new SearchTextObserver());

    // show recents options only for the albums fragment
    menu.findItem(R.id.action_show_recent_albums).setVisible(mMyMusicViewPager.getCurrentItem() == 1);

    super.onCreateOptionsMenu(menu, menuInflater);
}
 
Example 10
Source File: FilesFragment.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initialize the options menu.
 * Be sure to call {@link #setHasOptionsMenu} before.
 *
 * @param menu         The container for the custom options menu.
 * @param menuInflater The inflater to instantiate the layout.
 */
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.options_menu_files_fragment, menu);

    // get tint color
    int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_text_accent);

    Drawable drawable = menu.findItem(R.id.action_search).getIcon();
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, tintColor);
    menu.findItem(R.id.action_search).setIcon(drawable);

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    // Check if a search string is saved from before
    if (mSearchString != null) {
        // Expand the view
        searchView.setIconified(false);
        menu.findItem(R.id.action_search).expandActionView();
        // Set the query string
        searchView.setQuery(mSearchString, false);
        // Notify the adapter
        applyFilter(mSearchString);
    }

    searchView.setOnQueryTextListener(new SearchTextObserver());

    super.onCreateOptionsMenu(menu, menuInflater);
}
 
Example 11
Source File: ActivityMain.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
private void updateSearch(String search) {
    if (menuSearch != null) {
        SearchView searchView = (SearchView) menuSearch.getActionView();
        if (search == null) {
            if (menuSearch.isActionViewExpanded())
                adapter.getFilter().filter(searchView.getQuery().toString());
        } else {
            menuSearch.expandActionView();
            searchView.setQuery(search, true);
        }
    }
}