2

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 ?

Ajith S
  • 2,907
  • 1
  • 18
  • 30
kiran
  • 223
  • 4
  • 12
  • Pass the search query (String) in a bundle to the SearchActivity then you can expand the keyboard and fill the edit field of the search item for more searching – An-droid Oct 17 '13 at 07:45
  • I want it like Gmail, Play store app. the keyboard not expanded in the SearchActivity when it loads with result for the first time, Also What is the best place in the SearchActivity to set the query in SearchView - onCreateOptionMenu() method, or doSearch() ? – kiran Oct 17 '13 at 10:45

1 Answers1

0

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);
    }
}
An-droid
  • 6,433
  • 9
  • 48
  • 93
  • Thanks @Yume117, It helped. I am still not able to hide the keyboard when taken to the SearchActivity for the first time. I am setting searchquery in SearchView and calling closeKeyboard() in the onCreateOptionsMenu() callback. Also called closeKeyboard() in onQueryTextSubmit() which is working as expected. – kiran Oct 18 '13 at 10:59
  • I kind of copied the code from one of my project so my apologies if some i use methods or xml file that you don't have ^^ – An-droid Oct 18 '13 at 12:08
  • 1
    I understood your code and wrote similar method closeKeyboard(). I set the query in the SearchView and called menuItem.expandActionView() so that it is shown filled and expanded in the SearchActivity's actionbar, but the soft keyboard comes visible instead of hidden. I set android:windowSoftInputMode="stateHidden" in the manifest for the SearchActivity, which hides the keyboard when subsequent searches are performed on the SearchActivity - which is as desired. – kiran Oct 24 '13 at 13:20