Create Voice Search Android Application Using Android Studio

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, I will show you how to create a Voice Search android application using an android studio.
 
Requirements
Steps should be followed
 
These steps create a Voice Search Android application using Android studio and I have included the source code below.
 
Step 1
 
Open Android Studio and Start a New Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
 
Android
 
Add Activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml, This XML file contains the designing code for an Android app in the activity_main.xml;
 
Android
 
The XML code given below.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textView1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:paddingBottom="4dip"  
  13.         android:text="Click the button " />  
  14.   
  15.     <EditText  
  16.         android:id="@+id/editText1"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:ems="10" >  
  20.   
  21.         <requestFocus />  
  22.     </EditText>  
  23.   
  24.     <Button android:id="@+id/speakButton"  
  25.         android:layout_width="fill_parent"  
  26.         android:onClick="speakButtonClicked"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="Click here!" />  
  29.   
  30. </LinearLayout>  
Step 5
 
Go to  Main Activity.java, This Java program is the backend language for an Android.
 
Android
 
The java code is given below
  1.  package abu.myapplication;  
  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.   
  100. }  
Step 6
 
Now, go to the menu bar and click make a project or press ctrl+f9. To debug the error.
 
Android
 
Step 7
 
Then click Run button or press shift+f10 To run the project. And choose the virtual machine and click OK.
 
Android
 
Conclusion
 
We have successfully created a Voice Search Android application using the Android studio.
 
Android
 
Android


Similar Articles