Example usage for android.widget SearchView requestFocusFromTouch

List of usage examples for android.widget SearchView requestFocusFromTouch

Introduction

In this page you can find the example usage for android.widget SearchView requestFocusFromTouch.

Prototype

public final boolean requestFocusFromTouch() 

Source Link

Document

Call this to try to give focus to a specific view or to one of its descendants.

Usage

From source file:edu.ucsb.cs.cs185.inspirante.searchbyranking.SearchByRankingFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_search_by_ranking, container, false);

    final SearchView searchView = (SearchView) rootView.findViewById(R.id.search_by_ranking_search_view);
    int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchEditText = (EditText) searchView.findViewById(id);
    searchEditText.setTextColor(Color.GRAY);
    searchEditText.setHintTextColor(Color.GRAY);
    searchView.setOnQueryTextListener(searchViewListener);
    searchView.setMaxWidth(Integer.MAX_VALUE);
    searchView.setOnClickListener(new View.OnClickListener() {

        @Override//from   w w  w  . j  a v  a 2  s.c  o m
        public void onClick(View view) {
            searchView.setFocusable(true);
            searchView.setIconified(false);
            searchView.requestFocusFromTouch();
        }
    });

    mInspireRecyclerView = (RecyclerView) rootView.findViewById(R.id.search_rv);
    mInspireRecyclerView.setHasFixedSize(true);
    mInspireRecyclerView.setNestedScrollingEnabled(false);

    frequentTags = ItemCards.getInstance(getContext()).getFrequentTags();

    mInspireAdapter = new RankByTagAdapter(getContext(), this, frequentTags);
    mInspireAdapter.setHasStableIds(true);

    mLayoutManager = new LinearLayoutManager(getContext());
    mLayoutManager.setItemPrefetchEnabled(true);

    mInspireRecyclerView.setLayoutManager(mLayoutManager);

    mInspireAdapter.setOnItemClickListener(new RankByTagAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            TextView tv = (TextView) view;
            Intent intent = new Intent(getActivity(), TagAndImagesActivity.class);
            intent.putExtra("TAG", tv.getText());
            startActivity(intent);
        }
    });

    ItemCards itemCards = ItemCards.getInstance(getContext());
    itemCards.setAdapter(mInspireAdapter);

    mInspireRecyclerView.setAdapter(mInspireAdapter);
    mInspireAdapter.notifyDataSetChanged();

    // Set Tag recycler view
    mTagRecyclerView = (RecyclerView) rootView.findViewById(R.id.search_tags_rv);
    mTagRecyclerView.setHasFixedSize(true);
    mTagRecyclerView.setNestedScrollingEnabled(false);
    mTagsAdapter = new TagsAdapter(getContext(), frequentTags);
    mTagsAdapter.setHasStableIds(true);
    RecyclerView.LayoutManager tagsLayoutManager = new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, false);
    tagsLayoutManager.setItemPrefetchEnabled(true);

    mTagRecyclerView.setLayoutManager(tagsLayoutManager);

    mTagsAdapter.setOnItemClickListener(new TagsAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            String tag = mTagsAdapter.getTag(position);
            searchViewListener.onQueryTextChange(tag);
            //searchView.setFocus
            searchView.setQuery(tag, true);
            searchView.setIconified(false);
            searchView.clearFocus();
            mLayoutManager.scrollToPosition(0);

        }
    });

    mTagRecyclerView.setAdapter(mTagsAdapter);
    mTagsAdapter.notifyDataSetChanged();

    return rootView;
}

From source file:co.tinode.tindroid.ContactsFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.menu_contacts, menu);

    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // Retrieves the system search manager service
    final SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);

    // Retrieves the SearchView from the search menu item
    final SearchView searchView = (SearchView) searchItem.getActionView();
    // searchView.setFocusable(true);

    // Assign searchable info to SearchView
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

    // Set listeners for SearchView
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override/* w w  w.j  a  va 2s .  c  o  m*/
        public boolean onQueryTextSubmit(String queryText) {
            // Nothing needs to happen when the user submits the search string
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // Called when the action bar search text has changed.  Updates
            // the search filter, and restarts the loader to do a new query
            // using the new search string.
            String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

            // Don't do anything if the filter is empty
            if (mSearchTerm == null && newFilter == null) {
                return true;
            }

            // Don't do anything if the new filter is the same as the current filter
            if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                return true;
            }

            // Updates current filter to new filter
            mSearchTerm = newFilter;

            // Restarts the loader. This triggers onCreateLoader(), which builds the
            // necessary content Uri from mSearchTerm.
            getLoaderManager().restartLoader(ContactsQuery.CORE_QUERY_ID, null, mContactsLoaderCallback);
            return true;
        }
    });

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            Log.d(TAG, "EXPAND onMenuItemActionCollapse");
            searchView.setIconified(false);
            searchView.requestFocusFromTouch();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            Log.d(TAG, "COLLAPSE onMenuItemActionCollapse");
            searchView.clearFocus();
            // When the user collapses the SearchView the current search string is
            // cleared and the loader restarted.
            if (!TextUtils.isEmpty(mSearchTerm)) {
                onSelectionCleared();
            }
            mSearchTerm = null;
            getLoaderManager().restartLoader(ContactsQuery.CORE_QUERY_ID, null, mContactsLoaderCallback);
            return true;
        }
    });

    if (mSearchTerm != null) {
        // If search term is already set here then this fragment is
        // being restored from a saved state and the search menu item
        // needs to be expanded and populated again.

        // Stores the search term (as it will be wiped out by
        // onQueryTextChange() when the menu item is expanded).
        final String savedSearchTerm = mSearchTerm;

        // Expands the search menu item
        searchItem.expandActionView();

        // Sets the SearchView to the previous search string
        searchView.setQuery(savedSearchTerm, false);
    }
}