Use Voice Search in Android Studio

Let us see the steps on how to perform this task in android .Create new project with a valid name, once everything ready our projects looks like below screen.

Android Studio

App->res->layout->activity_main.xml

  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >  
  4.     <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" android:text="Click the button and start speaking" />  
  5.     <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" >  
  6.         <requestFocus/>  
  7.     </EditText>  
  8.     <Button android:id="@+id/speakButton" android:layout_width="fill_parent" android:onClick="speakButtonClicked" android:layout_height="wrap_content" android:text="Click Me!" />  
  9. </LinearLayout>  
app->java->com.example.pavithra.voicesearch->MainActivity.java

  1. package com.example.pavithra.voicesearch;  
  2. import android.content.Intent;  
  3. import android.content.pm.PackageManager;  
  4. import android.content.pm.ResolveInfo;  
  5. import android.speech.RecognizerIntent;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.os.Bundle;  
  8. import android.text.Editable;  
  9. import android.text.TextWatcher;  
  10. import android.view.View;  
  11. import android.widget.AdapterView;  
  12. import android.widget.ArrayAdapter;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.ListView;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18. import java.util.ArrayList;  
  19. import java.util.List;  
  20. public class MainActivity extends AppCompatActivity  
  21. {  
  22.     EditText ed;  
  23.     TextView tv;  
  24.     private static final int REQUEST_CODE = 1234;  
  25.     Button speak;@  
  26.     Override  
  27.     protected void onCreate(Bundle savedInstanceState)  
  28.         {  
  29.             super.onCreate(savedInstanceState);  
  30.             setContentView(R.layout.activity_main);  
  31.             final Button speak = (Button) findViewById(R.id.speakButton);  
  32.             ed = (EditText) this.findViewById(R.id.editText1);  
  33.             tv = (TextView) this.findViewById(R.id.textView1);  
  34.             // Disable button if no recognition service is present  
  35.             PackageManager pm = getPackageManager();  
  36.             List < ResolveInfo > activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  37.             if (activities.size() == 0)  
  38.             {  
  39.                 speak.setEnabled(false);  
  40.                 speak.setText("Recognizer not present");  
  41.             }  
  42.             ed.addTextChangedListener(new TextWatcher()  
  43.             {@  
  44.                 Override  
  45.                 public void beforeTextChanged(CharSequence s, int start, int count, int after)  
  46.                 {  
  47.                     // TODO Auto-generated method stub  
  48.                 }@  
  49.                 Override  
  50.                 public void onTextChanged(CharSequence s, int start, int before, int count)  
  51.                 {  
  52.                     // TODO Auto-generated method stub  
  53.                 }@  
  54.                 Override  
  55.                 public void afterTextChanged(Editable s)  
  56.                 {  
  57.                     // TODO Auto-generated method stub  
  58.                     speak.setEnabled(false);  
  59.                 }  
  60.             });  
  61.         }  
  62.         /** 
  63.          * Handle the action of the button being clicked 
  64.          */  
  65.     public void speakButtonClicked(View v)  
  66.         {  
  67.             startVoiceRecognitionActivity();  
  68.         }  
  69.         /** 
  70.          * Fire an intent to start the voice recognition activity. 
  71.          */  
  72.     private void startVoiceRecognitionActivity()  
  73.         {  
  74.             Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  75.             intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  76.             intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice searching...");  
  77.             startActivityForResult(intent, REQUEST_CODE);  
  78.         }  
  79.         /** 
  80.          * Handle the results from the voice recognition activity. 
  81.          */  
  82.         @  
  83.     Override  
  84.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
  85.     {  
  86.         if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)  
  87.         {  
  88.             // Populate the wordsList with the String values the recognition engine thought it heard  
  89.             final ArrayList < String > matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  90.             if (!matches.isEmpty())  
  91.             {  
  92.                 String Query = matches.get(0);  
  93.                 ed.setText(Query);  
  94.                 speak.setEnabled(false);  
  95.             }  
  96.         }  
  97.         super.onActivityResult(requestCode, resultCode, data);  
  98.     }  
  99. }  
Now Run the Application by pressing Shift+F10 and see the output as show like below

Voice search