How to implement Voice search to SearchView

fobus picture fobus · Jul 25, 2013 · Viewed 20.3k times · Source

I want to add voice search function to my application. I'm populating a SearchView in SherlockActivity. But I can't find a solution to add voice search function to SearchView object.

Can you please give an advice, what do I need to do?

Code below :

    public class MainActivity extends SherlockActivity {
        private SlidingMenu slidingMenu;
        private SlidingMenu slidingMenuRight;
        private String mFilterArrays[];
        public long lastScrollTime=0; /** En son kaydırma ne zaman yapıldı*/
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    }   

        public boolean onCreateOptionsMenu(Menu menu) {

                    //Create the search view
                    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
                    searchView.setQueryHint("Search...");


                    menu.add("Search")
                        .setIcon(R.drawable.ic_search_inverse)
                        .setActionView(searchView)
                        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
                        return true;
    }
}

Mainfest

<activity
    android:name="com.paea.bcp.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.paea.bcp.MainActivity" />
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.SEARCH" />                
    </intent-filter>
</activity>

Answer

Rahul Sainani picture Rahul Sainani · Jul 26, 2013

You can check out the documentation here

Update: If your SearchActivity is the same one, you can override onNewIntent and handle the Search intent there. Also make your activity as singleTop, that way only one instance of the activity will stay on top.

Hope this helps and please do notify if you find a solution.

Cheers!