Example usage for android.widget SearchView setOnQueryTextFocusChangeListener

List of usage examples for android.widget SearchView setOnQueryTextFocusChangeListener

Introduction

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

Prototype

public void setOnQueryTextFocusChangeListener(OnFocusChangeListener listener) 

Source Link

Document

Sets a listener to inform when the focus of the query text field changes.

Usage

From source file:at.wada811.android.library.demos.loader.LoaderListActivity.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void initSearchView(final MenuItem menuItem) {
    if (AndroidUtils.isMoreThanBuildVersion(Build.VERSION_CODES.ICE_CREAM_SANDWICH)) {
        final SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override//from  w  ww.  ja v  a 2 s  .c om
            public boolean onQueryTextSubmit(String query) {
                mLoaderListFragment.init();
                mLoaderListFragment.searchImageWithKeyword(query);
                menuItem.collapseActionView();
                getActionBar().setTitle(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    menuItem.collapseActionView();
                }
            }
        });
    }
}

From source file:de.enlightened.peris.PerisMain.java

@Override
public final boolean onCreateOptionsMenu(final Menu menu) {
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if (this.serverUserid == null || !getString(R.string.subforum_id).contentEquals("0")) {
        searchView.setVisibility(View.GONE);
    } else {//from w ww  . j  a v  a  2s  .  c  om
        if (ThemeSetter.getForegroundDark(this.background)) {
            searchMenuItem.setIcon(R.drawable.ic_action_search_dark);
        }
    }

    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(final View view, final boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        public boolean onQueryTextChange(final String newText) {
            // TODO Auto-generated method stub
            return false;
        }

        @SuppressWarnings("checkstyle:requirethis")
        public boolean onQueryTextSubmit(final String query) {
            if (getActionBar() != null) {
                getActionBar().setSubtitle(baseSubtitle);
            }
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);

            final Bundle bundle = new Bundle();
            bundle.putString("subforum_name", (String) "Search - " + query);
            bundle.putString("subforum_id", (String) "search");
            bundle.putString("query", (String) query);
            bundle.putString("background", (String) background);
            bundle.putString("icon", (String) "n/a");
            bundle.putString("inTab", (String) "N");
            loadCategory(bundle, "SEARCH_QUERY", false);

            return false;
        }
    });
    return true;
}

From source file:com.cypress.cysmart.CommonFragments.ProfileScanningFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();//from w  w w.j a v  a 2  s . c o  m
    inflater.inflate(R.menu.global, menu);
    MenuItem item = menu.findItem(R.id.graph);
    MenuItem log = menu.findItem(R.id.log);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    item.setVisible(false);
    log.setVisible(true);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false);
    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered

            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
                // this is your adapter that will be filtered
                if (mLeDeviceListAdapter != null) {
                    searchEnabled = true;
                    searchDeviceList(query);
                }
            }
            return false;

        }
    };
    searchView.setOnQueryTextListener(textChangeListener);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.tweetlanes.android.core.view.BaseLaneActivity.java

void configureActionBarSearchView(Menu menu) {

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

    searchItem.setShowAsActionFlags(//  www  .j  a v  a2 s.  c  o  m
            MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    searchView.setOnQueryTextListener(this);

    OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            mCurrentComposeFragment.setIgnoreFocusChange(true);

            if (mComposeTweetView != null) {
                mComposeTweetView.setVisibility(View.GONE);
            }
            if (mComposeDirectMessageView != null) {
                mComposeDirectMessageView.setVisibility(View.GONE);
            }

            if (!hasFocus) {
                if (mCurrentComposeFragment == mComposeDirectMessageFragment) {
                    if (mComposeDirectMessageView != null) {
                        mComposeDirectMessageView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mComposeTweetView != null) {
                        mComposeTweetView.setVisibility(View.VISIBLE);
                    }
                }
            }

            mCurrentComposeFragment.setIgnoreFocusChange(false);
        }

    };

    searchView.setOnQueryTextFocusChangeListener(onFocusChangeListener);
    searchView.setOnFocusChangeListener(onFocusChangeListener);
}