Create Text To Speech Conversion 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 Text to Speech conversion Android application using Android Studio. 

Requirements

Steps should be followed

Follow these steps to create a Text to Speech conversion 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 "Next" button.

Android

Now, select the version of Android and select the target Android devices.

Android

Step 3

Now, add the activity and click "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. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.   
  8.     tools:context="abu.text_to_speech.MainActivity" >  
  9.     <RelativeLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical">  
  13.     <EditText  
  14.         android:id="@+id/editText1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginTop="70dp"  
  18.         android:ems="10"  
  19.         android:inputType="textPersonName" />  
  20.         <Button  
  21.             android:id="@+id/button1"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_alignRight="@+id/editText1"  
  25.             android:layout_below="@+id/editText1"  
  26.             android:layout_marginRight="49dp"  
  27.             android:layout_marginTop="101dp"  
  28.             android:text="Button" />  
  29.     </RelativeLayout>  
  30. </android.support.constraint.ConstraintLayout>  

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.text_to_speech;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.speech.tts.TextToSpeech;  
  6. import android.speech.tts.TextToSpeech.OnInitListener;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13. public class MainActivity extends Activity implements OnInitListener {  
  14.     EditText ee;  
  15.     Button b1;  
  16.     TextToSpeech tts;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         ee=(EditText)findViewById(R.id.editText1);  
  22.         b1=(Button)findViewById(R.id.button1);  
  23.         b1.setOnClickListener(new OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 // TODO Auto-generated method stub  
  27.                 String str=ee.getText().toString();  
  28.                 tts.speak(str,TextToSpeech.QUEUE_FLUSH,null);  
  29.             }  
  30.         });  
  31.         Intent i =new Intent();  
  32.         i.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);  
  33.         startActivityForResult(i, 1);  
  34.     }  
  35.     @Override  
  36.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  37.         // TODO Auto-generated method stub  
  38.         super.onActivityResult(requestCode, resultCode, data);  
  39.         if(requestCode==1)  
  40.         {  
  41.             if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)  
  42.             {  
  43.                 tts= new TextToSpeech(this,this);  
  44.             }  
  45.             else  
  46.             {  
  47.                 Intent i=new Intent();  
  48.                 i.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);  
  49.                 startActivity(i);  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onInit(int status) {  
  56.         // TODO Auto-generated method stub  
  57.         if(status==TextToSpeech.SUCCESS)  
  58.         {  
  59.             Toast.makeText(getApplicationContext(), "engine installed",1000).show();  
  60.         }  
  61.         if(status==TextToSpeech.ERROR)  
  62.         {  
  63.             Toast.makeText(getApplicationContext(), "engine not installed", 1000).show();  
  64.         }  
  65.     }  
  66. }  

Step 6

Now,go to menu bar and click make 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 text to speech conversion Android application using Android studio.

Android

Up Next
    Ebook Download
    View all
    Learn
    View all