androidsearchviewsearchactivity

Android: Show search query in SearchView of a SearchActivity


I have few activities in my app which are meant to perform some specific tasks. I would like to have a search features on almost all of these activities.

My design is as below:

Activity1: With SearchView widget in Actionbar

Activity2: With SearchView widget in Actionbar

SearchActivity: Which perform the search and display results in ListView.

I have done necessary configuration in my manifest file for the SearchActivity.

When I touch on the search icon(magnifying glass), the SearchView gets expanded to allow user to enter the query,then user trigger the search and search intent got delivered to the SearchActivity.

Since the user originally entered the search query on Activity, I don't see the query and SearchView in my SearchActivity (this is basically required to allow user to perform further searches).

I understand that to do so I will also need a SearchView in my SearchActivity too, but How to pre-populate it with the query entered in previous activity and show in expanded state ?


Solution

  • I usually do things like that (plus hving the adapter implements Filterable):

    public class ActivityFiltrable extends SherlockFragmentActivity implements OnQueryTextListener{
    
        protected String    _searchCurrentQuery;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.activity);
            // ABS
            ActionBar bar = getSupportActionBar();
            bar.setTitle(R.string.tle_search);
            bar.setDisplayHomeAsUpEnabled(true);
    
            //DO STUFF
            super.onCreate(savedInstanceState);
        }
    
        @Override
        protected void onDestroy() {
            closeKeyboard(this);
            super.onDestroy();
        }
        @Override
        public void onBackPressed() {
            finish();
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();
            getSupportMenuInflater().inflate(R.menu.action_bar_search, menu);
            // SEARCH MENUITEM
            MenuItem searchItem = menu.findItem(R.id.menu_search);
            SearchView searchView = (SearchView) searchItem.getActionView();
            searchView.setSubmitButtonEnabled(true);
            searchView.setOnQueryTextListener(this);
            return super.onPrepareOptionsMenu(menu);
        }
    
        public static void closeKeyboard(Context context) {
            try {
                InputMethodManager inputManager = (InputMethodManager) ((SherlockFragmentActivity) context).getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(((SherlockFragmentActivity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
            catch (Exception e) {
                // TODO:ERROR CLOSING KEYBOARD
            }
        }
    
        /**
         * SEARCH
         */
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchAction(query);
            closeKeyboard(this);
            return true;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            searchAction(newText);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    if (item.getItemId() == android.R.id.home) {
                        finish();
                        // overridePendingTransition(R.animator.anim_left, R.animator.anim_right);
                        return true;
                    }
                    break;
        }
    
        protected void searchAction(String query) {
            _searchCurrentQuery = query.toString();
            EtablissementApplication._adapterFilterSearch.getFilter().filter(_searchCurrentQuery);
        }
    }