Speech to Text Application in Android with Kotlin
Last Updated :
01 Jun, 2022
Speech to Text is seen in many applications such as Google search. With the help of this feature, the user can simply speak the query he wants to search. The text format of that speech will be automatically generated in the search bar. In this article, we will be taking a look at How to implement Speech Text in Android applications using Kotlin.
Note: If you want to implement speech to text in android application using java, Check out the following article: Speech to Text in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add mic image in drawable folder
Navigate to the app > res > drawable > Right-click on it. Then click on New > Vector Asset. After that click on the clip art icon. Inside that simply search for the mic icon and we will get to see the icon. After that, we will have to rename the icon as ic_mic. After adding this simply click on Finish to add this icon to the drawable folder.
Step 3: 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. Comments are added inside the code to understand the code in more detail.
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--creating a simple image view for mic-->
<ImageView
android:id="@+id/idIVMic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:src="@drawable/ic_mic"
android:tint="@color/purple_200" />
<!--creating text view on below line-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idIVMic"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Click on Mic to speak"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp" />
<!--creating a text view for displaying
output on below line-->
<TextView
android:id="@+id/idTVOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idIVMic"
android:layout_marginTop="70dp"
android:gravity="center"
android:text="Output will appear here"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
</RelativeLayout>
Step 4: 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.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
// on below line we are creating variables
// for text view and image view
lateinit var outputTV: TextView
lateinit var micIV: ImageView
// on below line we are creating a constant value
private val REQUEST_CODE_SPEECH_INPUT = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initializing variables of list view with their ids.
outputTV = findViewById(R.id.idTVOutput)
micIV = findViewById(R.id.idIVMic)
// on below line we are adding on click
// listener for mic image view.
micIV.setOnClickListener {
// on below line we are calling speech recognizer intent.
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
// on below line we are passing language model
// and model free form in our intent
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
// on below line we are passing our
// language as a default language.
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault()
)
// on below line we are specifying a prompt
// message as speak to text on below line.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")
// on below line we are specifying a try catch block.
// in this block we are calling a start activity
// for result method and passing our result code.
try {
startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT)
} catch (e: Exception) {
// on below line we are displaying error message in toast
Toast
.makeText(
this@MainActivity, " " + e.message,
Toast.LENGTH_SHORT
)
.show()
}
}
}
// on below line we are calling on activity result method.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// in this method we are checking request
// code with our result code.
if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
// on below line we are checking if result code is ok
if (resultCode == RESULT_OK && data != null) {
// in that case we are extracting the
// data from our array list
val res: ArrayList<String> =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) as ArrayList<String>
// on below line we are setting data
// to our output text view.
outputTV.setText(
Objects.requireNonNull(res)[0]
)
}
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
Speech to Text Application in Android using Jetpack Compose Speech to Text is used in most applications such as Google Search for searching any query. For using this feature user simply has to tap on the microphone icon and speak the query he wants to search. The speech of the user will be converted to text. In this article, we will take a look at How we can
7 min read
How to Convert Text to Speech in Android using Kotlin? 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
3 min read
Offline Speech to Text Without any Popup Dialog in Android In this article, we are going to implement an offline speech-to-text functionality in our project. It can work both Online and Offline. When there is no internet connectivity, it will use the pre-stored language model from our mobile device, so it didn't recognize much clearly but gave good results.
6 min read
Chatting Application in Android with Kotlin Building a Real-Time Chat Application can be a great way to dive into Android Development and Firebase. In this article, we will learn to make a basic chat application using Android Studio IDE with Kotlin and real-time chatting using Google's FirebaseFirestore as a database.Pre-requisitesAndroid Stu
7 min read
How to Convert Text to Speech in Android? 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
3 min read
How to Convert Speech to Text in Android? In this article, speech to text feature is implemented in an application in Android. Speech to text means that anything that the user says is converted into text. This feature has come out to be a very common and useful feature for the users. In various places where search feature is implemented lik
5 min read
Calendar View App in Android with Kotlin Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used. In this article, we will take a look at How to implement Calendar View within our Android application using Kotlin. A sample video is g
3 min read
First Android Application in Kotlin We can build an Android application using Kotlin and Java. In the Android Studio Kotlin Tutorial, we are using Kotlin language to build the application. In the previous tutorial, we learned how to create a project for Kotlin language but here, we will learn how to run an application using the AVD (A
2 min read
SearchView in Android with Kotlin SearchView is a widget in android which provides a search interface with the help of which users can be able to make searches within the given list of data. In this search view, the user has to specify the search query. According to the search, query results will be populated within the listview. In
4 min read
Android - Update Data in API using Volley with Kotlin Android applications use APIs to get the data from servers in android applications. With the help of APIs, we can add, read, update and delete the data from our database using APIs. We can use Volley and Retrofit for consuming data from APIs within the android application. In this article, we will t
5 min read