Android Text To Speech Voice Example tutorial with source code download

How to create an android app which will get EditText value and convert text into audio speech programmatically.

Android mobile operating system covers more then 40% market of mobile devices and increasing day by day. There are millions of android apps available on Google play store so in this tutorial we are going to do something new and design a simple android app which known as Android Text To Speech Voice Example tutorial with text conversion to direct speech . With this android application android developers can gives the facility to its user so they can convert EditText typed text values to speech . So here is the complete step by step tutorial for Android Text To Speech Voice Example tutorial with source code download .

android-project-download-code-button

Android Text To Speech Voice Example tutorial with source code download.

Code for MainActivity.java file.

package com.android_examples.text2speech_android_examplescom;
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 android.widget.Toast;
import java.util.Locale;

public class MainActivity extends AppCompatActivity
        implements TextToSpeech.OnInitListener {

    TextToSpeech textToSpeech;
    Button button;
    EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textToSpeech = new TextToSpeech(MainActivity.this, MainActivity.this);

        button = (Button) findViewById(R.id.button);

        editText = (EditText) findViewById(R.id.editText);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                TextToSpeechFunction() ;
            }

        });
    }

    public void TextToSpeechFunction()
    {

        String textholder = editText.getText().toString();

        textToSpeech.speak(textholder, TextToSpeech.QUEUE_FLUSH, null);

        Toast.makeText(MainActivity.this , textholder, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {

        textToSpeech.shutdown();

        super.onDestroy();
    }

    @Override
    public void onInit(int Text2SpeechCurrentStatus) {

        if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {

            textToSpeech.setLanguage(Locale.US);

                button.setEnabled(true);

                TextToSpeechFunction();
            }

    }
}

Code for activity_main.xml layout file.

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.text2speech_android_examplescom.MainActivity">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="Enter Text Here"
        android:gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click here to convert EditText text into speech"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp" />
</RelativeLayout>

Android Text To Speech Voice Example tutorial with source code download

 

 Click here to download Android Text To Speech Voice Example tutorial with source code download.

19 Comments

  1. Hey, Thank You For this Tutorial ! Keep sharing man.

  2. Thx for your TuT Sir.

    I have a question, is it possible to do text to speech with no button? like if i get a notification from friebase text to speech tells me what message received? i can’t find a tutorial to do it with no button. will be cool if you can explain us how it works. Thx

    • Yes STEFFEN just send convert the whole message into String then at the activity start time parse that message into text to speech to whenever the activity opens it will start playing message.

  3. DELETE ALL THE REPLAYS ITS WORKING LIKE I TELLED YOU 😀

  4. @JUNED MUGHAL

    I have only one last question to you 🙂
    can you tell me how i c an safe my switch, like i turn it off, after i close my app and going back its on again, how can i safe it if i do it off it stays off.

    Thx Sir!

    • Hey read my this post this would help you https://www.android-examples.com/detect-switch-button-is-on-or-off-in-android-dynamically/ now inside the scope use finish() method to exit from current opening activity.

      • Thx for your Replay. I do it like you do, but after i restarted my App it´s again disabled the switch, but i turned it on before is closed the app -.-

        isTextToSpeechErlaubt = (Switch) findViewById(R.id.switch1);
        isTextToSpeechErlaubt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked == true){
        FirebaseMessagingService p = new FirebaseMessagingService();
        p.isTextToSpeechErlaubt = true;
        // finish();
        }else{
        FirebaseMessagingService p = new FirebaseMessagingService();
        p.isTextToSpeechErlaubt = false;
        }
        }
        });

        finish do close the app and not saving the switch on off if i turn it off… how to safe the bollean status ?

        Thx man!

  5. Hey Ho,

    Im working still on myProblem, can pls check the Code? i don#t know its crashing all time if i use this line to load the settings on App_Start:

    isTextToSpeech.setChecked(sharedPrefs.getBoolean(“isTextToSpeech”, true));

    this crash my App it’s saying in debugger:

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.Switch.setChecked(boolean)’ on a null object reference.

    i don#t see any error.

    here the fully source, using: SharedPreferences to safe the on or off method.

    SharedPreferences sharedPrefs = getSharedPreferences(“com.android.Test”, MODE_PRIVATE);
    isTextToSpeech.setChecked(sharedPrefs.getBoolean(“isTextToSpeech”, true));

    isTextToSpeech = (Switch) findViewById(R.id.switch1);
    isTextToSpeech.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked == true){
    FirebaseMessagingService p = new FirebaseMessagingService();
    p.isTextToSpeechEclat = true;
    SharedPreferences.Editor editor = getSharedPreferences(“com.android_examples.INFO”, MODE_PRIVATE).edit();
    editor.putBoolean(“isTextToSpeech”, true);
    editor.commit();
    }else{
    FirebaseMessagingService p = new FirebaseMessagingService();
    p.isTextToSpeechEclat = false;
    SharedPreferences.Editor editor = getSharedPreferences(“com.example.xyz”, MODE_PRIVATE).edit();
    editor.putBoolean(“isTextToSpeech”, false);
    editor.commit();
    }
    }
    });

    you see a problem ? me not. Pls ignore my first Post, thx!

  6. im sorry,

    SharedPreferences sharedPrefs = getSharedPreferences(“com.android.Test”, MODE_PRIVATE);
    isTextToSpeech.setChecked(sharedPrefs.getBoolean(“isTextToSpeech”, true));

    isTextToSpeech = (Switch) findViewById(R.id.switch1);
    isTextToSpeech.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked == true){
    FirebaseMessagingService p = new FirebaseMessagingService();
    p.isTextToSpeechEclat = true;
    SharedPreferences.Editor editor = getSharedPreferences(“com.android.Test”, MODE_PRIVATE).edit();
    editor.putBoolean(“isTextToSpeech”, true);
    editor.commit();
    }else{
    FirebaseMessagingService p = new FirebaseMessagingService();
    p.isTextToSpeechEclat = false;
    SharedPreferences.Editor editor = getSharedPreferences(“com.android.Test”, MODE_PRIVATE).edit();
    editor.putBoolean(“isTextToSpeech”, false);
    editor.commit();
    }
    }
    });

  7. Thank you for your hard work!

Leave a Reply

Your email address will not be published. Required fields are marked *