Skip to content Skip to sidebar Skip to footer

How To Set The Menuitemclicklistener For A Searchview At A Secondary Toolbar

i am trying for hours to setup a simple android.support.v7.widget.SearchView in a bottom (secondary) android.support.v7.widget.ToolBar. I am already using another Toolbar up top se

Solution 1:

Updated 23/09/15 I could not find any good solution, so, since i only wanted to get the SearchView item i decided to hack into it like so:

protectedvoidonCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState==null){
        FragmentManagerfragmentManager= getSupportFragmentManager();
        FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.dlHomepageSpace, HomepageFragment.newInstance(mLanguage), HOMEPAGE_BUTTONS_TAG);
        fragmentTransaction.commit();
    }

    mSearchState = SearchState.NOSEARCH;

    mBottomToolbar = (Toolbar) findViewById(R.id.bottomToolbar);
    mBottomToolbar.inflateMenu(R.menu.bottom_toolbar);

    // Here we set the search view toolbar functionsif(mBottomToolbar.getMenu().size() > 0){

        SearchManagersearchManager= (SearchManager) HomepageActivity.this.getSystemService(Context.SEARCH_SERVICE);
        MenuItemsearchMenuItem= mBottomToolbar.getMenu().getItem(mBottomToolbar.getMenu().size()-1);
        mSearchView = (SearchView) searchMenuItem.getActionView();
        if (mSearchView != null) {
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(HomepageActivity.this.getComponentName()));
        }

        mSearchView.setOnCloseListener(newOnCloseListener() {

            @OverridepublicbooleanonClose() {
                Log.d(TAG, "onClose");
                   mBottomToolbar.getMenu().getItem(mBottomToolbar.getMenu().size()-1).collapseActionView();
                switchToFab();
                returnfalse;
            }
        });

        MenuItemCompat.setOnActionExpandListener(searchMenuItem, newMenuItemCompat.OnActionExpandListener() {

            @OverridepublicbooleanonMenuItemActionExpand(MenuItem item) {
                Log.d(TAG, "onMenuItemActionExpand");
                returntrue; // KEEP IT TO TRUE OR IT DOESN'T OPEN !!
            }

            @OverridepublicbooleanonMenuItemActionCollapse(MenuItem item) {
                Log.d(TAG, "onMenuItemActionCollapse");
                switchToFab();
                returntrue;
            }
        });
    }

}

@OverrideprotectedvoidonNewIntent(Intent intent) {
    Log.d(TAG, "onNewIntent");
    if(isSearchAction(intent)){

        ArrayList<Detail> list = (ArrayList<Detail>) getListData(intent.getStringExtra(SearchManager.QUERY));

        if(list.isEmpty()){
                Toast.makeText(this, R.string.no_search_results_en, Toast.LENGTH_LONG).show();

            }
        } else {

            ExtendedListFragmentsearchFragment= (ExtendedListFragment) getSupportFragmentManager().findFragmentByTag(HOMEPAGE_SEARCH_TAG);
            mResultsList = list;
            mSearchState = SearchState.SEARCH_LIST_SCREEN;

            if(searchFragment==null){
                FragmentTransactionfragmentTransaction= getSupportFragmentManager().beginTransaction();
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.replace(R.id.dlHomepageSpace, ExtendedListFragment.newInstance(list), HOMEPAGE_SEARCH_TAG);
                fragmentTransaction.commit();
            } else {
                searchFragment.setDetailList(list);
            }
        }
    }
}

privatebooleanisSearchAction(Intent intent) {
    Log.d(TAG, "handleIntent");
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.d(TAG, "ACTION_SEARCH");
        returntrue;
    }
    returnfalse;
}

In essence, i just look into the toolbar, and obtain the one and only Item into it, i cast it as a MenuItem and then i get the SearchView as the ActionView of the MenuItem. Like that it works and i do not have to use setOnMenuItemClickListener() . When i tap in the view, i can type into it (i do not need a mSearchView.OnQueryTextListener but, i have seen it working) , when i press the magnifying glass on the keyboard i get the onNewIntent() call working.

Post a Comment for "How To Set The Menuitemclicklistener For A Searchview At A Secondary Toolbar"