First you need to create android project then put this XML file in the path:
res/layout
in this file we will create a button to receive our request to speak and a list view which will list the suggested results for our speech.
Then, create a class named VoiceReco in your src path on this activity
#see comments in source code blow
First : we have to set up our layout and the list view , set click listener to our button which invoke on click method.
Second: in onclick method we make an intent which will fire speech recognition by passing an action called ACTION_RECOGNIZE_SPEECH
Third : passing some data to our intent such as
- Inform the recognizer which speech model to prefer when performing.
- Optional text prompt to show the user when asking them to speak.
- startActivityForResult
Finally we have to override onActivityResult method which will receive the result of the speech recognizer
-
check if the request sent successfully .
-
receive results in an array list of strings.
-
then show the results in a list view by using Array adapter.
Note : this example may not work on your android emulator but it will work fine on your android phone.