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
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
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. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="30dp"
  12. android:textAlignment="center"
  13. android:text="Voice 2 Text"
  14. android:textSize="20sp"
  15. android:textStyle="bold" />
  16. <EditText
  17. android:id="@+id/textbox"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_centerVertical="true"
  21. android:hint="Text will appear here"
  22. />
  23. <ImageButton
  24. android:id="@+id/voice_btn"
  25. android:layout_width="70dp"
  26. android:layout_height="70dp"
  27. android:layout_alignParentBottom="true"
  28. android:layout_centerHorizontal="true"
  29. android:layout_marginBottom="20dp"
  30. android:background="@drawable/microphone" />
  31. </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. import android.content.ActivityNotFoundException;
  3. import android.content.Intent;
  4. import android.speech.RecognizerIntent;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.ImageButton;
  10. import java.util.ArrayList;
  11. import java.util.Locale;
  12. public class MainActivity extends AppCompatActivity {
  13. EditText textbox;
  14. ImageButton voice_btn;
  15. final int VOICE_CODE = 100;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. textbox = (EditText) findViewById(R.id.textbox);
  21. voice_btn = (ImageButton) findViewById(R.id.voice_btn);
  22. voice_btn.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View view) {
  25. voice_to_text();
  26. }
  27. });
  28. }
  29. private void voice_to_text() {
  30. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  31. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  32. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  33. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  34. intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
  35. "Voice2Text \n Say Something!!");
  36. try {
  37. startActivityForResult(intent, VOICE_CODE);
  38. } catch (ActivityNotFoundException e) {
  39. }
  40. }
  41. // receive voice input and set it to textbox
  42. @Override
  43. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  44. super.onActivityResult(requestCode, resultCode, data);
  45. switch (requestCode) {
  46. case VOICE_CODE: {
  47. if (resultCode == RESULT_OK && null != data) {
  48. ArrayList<String> result = data
  49. .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  50. textbox.setText(result.get(0));
  51. }
  52. break;
  53. }
  54. }
  55. }
  56. }
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