Kotlin Text to Speech Application

Kotlin Text to Speech Application | Working Example Kotlin

In this Android text to speech example, I will explain how you can build a Kotlin Text to Speech Application in Android Studio using Kotlin Language. You write any text in the text box and the application will speak that text with just one click :). So, let’s start our Kotlin Text to Speech Application.

What is Android Text to Speech

TextToSpeech class in Android converts the Text into Speech. It converts the given text into speech form. After using this class use its method shutdown() to stop it completely.

Kotlin Text to Speech Application

1- Start a new Android Studio Project and name it “Text_To_Speech_New” or you can name it whatever you want. Select API level 19 and the default language should be Kotlin.

2- After that, go to activity_main.xml and change the default Constraint Layout to Relative Layout.

3- So, in your activity_main.xml file you need some views. A Text View, Edit Text, Button, and a Seek bar.

4- We are using Edit Text to write the text, Button to Listen to the Speech, and then a Seek bar to set up the speed and pitch of the sound, and finally two TextView(s) to display the Pitch and Speed.

5- After setting up all these views our final activity_main.xml will look like this.



MainActivity.kt Code


6- Now, it’s the time to play with our Kotlin code 🙂 …

7- So, go to your MainActivity.kt file, and after that first define all the views you are using in your XML.


8- We initialize all these views in a separate method called “initializeFields()”. Our method will look like this.


9- After this, we will call this method in our onCreate method of the Main Activity.

10- Now, in our onCreate method we will initialize and setup an OnItListener on our TextToSpeech.

11- Now we will perform every different task in a separate Method. We need three methods here. One to Enable speech, another one to stop the speech once the Activity destroys, and one method to close the keyboard after writing the text in our text box.

12- In speak() method we will define the speak functionality and then set up the Pitch and Speed of our spoken text.

13- Our onDestroy() method will end the speech if the speech is in progress and the user closes the application. Our method will look like this.

14- After writing the text in our text box when we click the button to listen to the text in audio this method will close our soft keyboard.

15- So, our final complete MainActivity.kt file will look like this.



Here you go, Enter any text in the Text Box and press the Button. The Text_To_Speech functionality will speak your text. Enjoy 🙂 … 





Kotlin Text to Speech Application
Kotlin Text to Speech Application


Kotlin Text to Speech Application
Kotlin Text to Speech Application



More Android Examples

1 thought on “Kotlin Text to Speech Application | Working Example Kotlin”

  1. Hi and thanks a lot for your clear auto, it helps me.
    Hower, it remain a question for me :
    I use ExoPlayer and TextToSpeech together.
    I use ExoPlayer to show videos.
    I use TextToSpeech to talk the title of the video at the time of playing.

    The problem is that I need to wipe Data from the emulator every time a launch the app to hear the first title, otherwise TextToSpeech don’t work.

    Any advice will be welcome and appreciate.

Leave a Comment

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

error: Content is protected !!