Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity with SearchView open

how can I start an activity with a SearchView with the text field open. In this way I could search without having to click on the spyglass. I am using Sherlock Action Bar.

Here is my code:

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {

     final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Cerca domande");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                if (query.length() != 0) {

                    // handle search here
                    return true;
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                 if (newText.length()>=3) {
                     new PostTask().execute(newText);   

                     Util.setSharedPreferences(SearchActivity.this, "lastsearch", newText);
                 }
                return false;
            }
        });

        menu.add("Search")
            .setIcon(ic_search_inverse)
            .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);


        listsearch.setOnItemClickListener(this);

     return true;
    }

Thanks.

like image 311
Gyonder Avatar asked Mar 02 '13 08:03

Gyonder


People also ask

How do I use Start activity?

To create the second activity, follow these steps: In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name. Leave all other properties set to their defaults and click Finish.

How do I start an activity from intent?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


1 Answers

Tried this?

searchMenu.expandActionView();

in OnCreateOptionsMenu()

menu.add("Search")
            .setIcon(ic_search_inverse)
            .setActionView(searchView)
            .expandActionView();
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

you can add focus, open soft-keyboard. So, that everything is ready for user to type in and search.

But this isn't the standard behavior.

like image 126
Archie.bpgc Avatar answered Oct 13 '22 07:10

Archie.bpgc