Voice 2 Text Android Application Tutorial

Introduction

 
Hello all, in this article, we will learn how to create an Android Voice2Text App. So by the app name, we can understand what we will be going to make. In this app Voice2Text, the user voice will be converted into text and will be displayed in the input field. Voice2Text App does not need any internet connection; it will work offline.
 
Requirements
  • Android Studio 2.3.3
  • Little knowledge of Java and XML
  • Android Phone to test the android app ()
Let’s get started,
 
Let’s start creating Voice2Text android app using Android Studio. I have given the download link of the project, you can find that below.
 
Step 1 - Creating a New Project with Android studio
  • Open Android Studio and create a new project, name it Voice2Text and company domain whatever you like for eg: foo.android (You can use your own name also)
  • Click Next and choose Min SDK; I have chosen Android 4.1 (Jelly Bean)
  • Click Next and select Empty Activity
  • Choose the Activity as Main Activity and click next and finish.
     
    Android
     
    Android
Once we create our New Project Gradle it will take some time to sync your project and will resolve all the dependencies. Sometimes this also takes a lot of lot of time.
 
Step 2 - Creating Layout of Voice2Text App
 
Here we will create our layout for our app. So, for tutorial purposes, I am keeping it simple. We will create a Text View at the top which will have our android app name, then an EditText with a voice from the text will be entered and finally an ImageButton by which we will open google voice to recognize our voice and convert it to text and enter it into the edit text.
 
XML Code for our Layout,
 
activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="12dp"  
  7.     tools:context="in.amitsin6h.voice2text.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginTop="30dp"  
  13.         android:textAlignment="center"  
  14.         android:text="Voice 2 Text"  
  15.         android:textSize="20sp"  
  16.         android:textStyle="bold" />  
  17.   
  18.     <EditText  
  19.         android:id="@+id/textbox"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_centerVertical="true"  
  23.         android:hint="Text will appear here"  
  24.         />  
  25.   
  26.     <ImageButton  
  27.         android:id="@+id/voice_btn"  
  28.         android:layout_width="70dp"  
  29.         android:layout_height="70dp"  
  30.         android:layout_alignParentBottom="true"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginBottom="20dp"  
  33.         android:background="@drawable/microphone" />  
  34.   
  35. </RelativeLayout>  
 
Step 3 - Voice2Text Android App Java Code
 
This is our main part where we will code our Voice2Text app for this we are using RecognizerIntent to convert voice into text. All you need to do is copy the below java code and paste it to MainActivity.java.
 
MainActivity.java
  1. package in.amitsin6h.voice2text;  
  2.   
  3.   
  4. import android.content.ActivityNotFoundException;  
  5. import android.content.Intent;  
  6. import android.speech.RecognizerIntent;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.EditText;  
  11. import android.widget.ImageButton;  
  12. import java.util.ArrayList;  
  13. import java.util.Locale;  
  14.   
  15. public class MainActivity extends AppCompatActivity {  
  16.   
  17.     EditText textbox;  
  18.     ImageButton voice_btn;  
  19.     final int VOICE_CODE = 100;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         textbox = (EditText) findViewById(R.id.textbox);  
  26.         voice_btn = (ImageButton) findViewById(R.id.voice_btn);  
  27.   
  28.         voice_btn.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 voice_to_text();  
  32.             }  
  33.         });  
  34.   
  35.     }  
  36.   
  37.   
  38.     private void voice_to_text() {  
  39.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  40.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  41.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  42.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());  
  43.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT,  
  44.                 "Voice2Text \n Say Something!!");  
  45.         try {  
  46.             startActivityForResult(intent, VOICE_CODE);  
  47.         } catch (ActivityNotFoundException e) {  
  48.   
  49.         }  
  50.     }  
  51.   
  52.     // receive voice input and set it to textbox  
  53.   
  54.     @Override  
  55.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  56.         super.onActivityResult(requestCode, resultCode, data);  
  57.   
  58.         switch (requestCode) {  
  59.             case VOICE_CODE: {  
  60.                 if (resultCode == RESULT_OK && null != data) {  
  61.   
  62.                     ArrayList<String> result = data  
  63.                             .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  64.                     textbox.setText(result.get(0));  
  65.                 }  
  66.                 break;  
  67.             }  
  68.   
  69.         }  
  70.     }  
  71.   
  72. }  
 
If anyone faces a problem to understand the java code you can comment below and I will help you guys understand.
 
Step 4 - Compile and Run
 
Now we are ready to compile and run our Voice2Text Android app. The following screen will appear once our app gets installed.
 
Android
 
Android
 
Android
 
Project Source Code


Similar Articles