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, 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.
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 time.
Step 2 - Creating Layout of Voice2Text App
Here we will create our layout for our app. So, for the 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 voice from 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 edittext.
XML Code for our Layout,
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="12dp"
- tools:context="in.amitsin6h.voice2text.MainActivity">
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="30dp"
- android:textAlignment="center"
- android:text="Voice 2 Text"
- android:textSize="20sp"
- android:textStyle="bold" />
-
- <EditText
- android:id="@+id/textbox"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:hint="Text will appear here"
- />
-
- <ImageButton
- android:id="@+id/voice_btn"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="20dp"
- android:background="@drawable/microphone" />
-
- </RelativeLayout>
Step 3 - Voice2Text Android App Java Code
This is our main part were 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
- package in.amitsin6h.voice2text;
-
-
- import android.content.ActivityNotFoundException;
- import android.content.Intent;
- import android.speech.RecognizerIntent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import java.util.ArrayList;
- import java.util.Locale;
-
- public class MainActivity extends AppCompatActivity {
-
- EditText textbox;
- ImageButton voice_btn;
- final int VOICE_CODE = 100;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textbox = (EditText) findViewById(R.id.textbox);
- voice_btn = (ImageButton) findViewById(R.id.voice_btn);
-
- voice_btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- voice_to_text();
- }
- });
-
- }
-
-
- private void voice_to_text() {
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
- "Voice2Text \n Say Something!!");
- try {
- startActivityForResult(intent, VOICE_CODE);
- } catch (ActivityNotFoundException e) {
-
- }
- }
-
-
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
- switch (requestCode) {
- case VOICE_CODE: {
- if (resultCode == RESULT_OK && null != data) {
-
- ArrayList<String> result = data
- .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- textbox.setText(result.get(0));
- }
- break;
- }
-
- }
- }
-
- }
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.
Project Source Code