How to create android Text To Speech app




Learn to create android text to speech app in android studio.It uses TextToSpeech class. You can use this in any app to speak your written text, reminder, notes and to create talkitive app.

MainActivity.java


 package com.singh.jassi.texttospeech;  
 import android.speech.tts.TextToSpeech;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import java.util.Locale;  
 public class MainActivity extends AppCompatActivity {  
   TextToSpeech tt;  
   Button b1;  
   EditText ed;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     tt=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener()  
     {  
       public void onInit(int status)  
       {  
         if(status!=TextToSpeech.ERROR)  
           tt.setLanguage(Locale.UK);  
       }  
     });  
     b1=(Button)findViewById(R.id.button);  
     ed=(EditText)findViewById(R.id.editText);  
   }  
   public void speak_my_text(View vv)  
   {  
     String string =ed.getText().toString();  
     tt.speak(string,TextToSpeech.QUEUE_FLUSH,null,null);  
   }  
 }  



actvity_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:id="@+id/activity_main"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   tools:context="com.singh.jassi.texttospeech.MainActivity">  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:inputType="textPersonName"  
     android:hint="Enter your text here..."  
     android:ems="10"  
     android:layout_marginTop="83dp"  
     android:id="@+id/editText"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true" />  
   <Button  
     android:text="Speak"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_marginBottom="123dp"  
     android:id="@+id/button"  
     android:layout_alignParentBottom="true"  
     android:layout_alignParentStart="true"  
     android:layout_marginStart="120dp"  
     android:onClick="speak_my_text"/>  
 </RelativeLayout>  
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.singh.jassi.texttospeech" >  
   <application  
     android:allowBackup="true"  
     android:icon="@mipmap/ic_launcher"  
     android:label="@string/app_name"  
     android:supportsRtl="true"  
     android:theme="@style/AppTheme" >  
     <activity android:name=".MainActivity" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Comments

Popular posts from this blog

File handling

Getting started with android app development