Skip to content
Related Articles

Related Articles

How to Convert Text to Speech in Android using Kotlin?

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Mar, 2022

Text to Speech App converts the text written on the screen to speech like you have written “Hello World” on the screen and when you press the button it will speak “Hello World”. Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it’s also convenient for those who want to be read too. This feature has come out to be a very common and useful feature for users. We will use Kotlin language for this project. In this article, we will see how to use speak() method of android.speech.tts.TextToSpeech class. And using the same we will convert a text to speech (audio).

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintVertical_bias="0.134" />
            android:ems="10" >
            android:text="@string/speak" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package com.ayush.myapplication
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Button
import android.widget.EditText
import java.util.*
// Extending MainActivity TextToSpeech.OnInitListener class
class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener {
    private var tts: TextToSpeech? = null
    private var btnSpeak: Button? = null
    private var etSpeak: EditText? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        // view binding button and edit text
        btnSpeak = findViewById(
        etSpeak = findViewById(
        btnSpeak!!.isEnabled = false
        // TextToSpeech(Context: this, OnInitListener: this)
        tts = TextToSpeech(this, this
        btnSpeak!!.setOnClickListener { speakOut() }
    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            val result = tts!!.setLanguage(Locale.US)
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language not supported!")
            } else {
                btnSpeak!!.isEnabled = true
    private fun speakOut() {
        val text = etSpeak!!.text.toString()
        tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null,"")
    public override fun onDestroy() {
        // Shutdown TTS when 
        // activity is destroyed
        if (tts != null) {

Note: We need to stop and shut down TextToSpeech Engine when activity is destroyed.

So, our app is ready. And we can see the output.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!