How to use Speech to Text in Android

How to use Speech to Text in Android

Hello everyone, in this article we are going to talk about SpeechRecognizer class for Speech to Text specification in android. We are going to make an example and implement Speech Recognizer class with Java language in Android Studio.

Let's get started.

Before we start to write codes first we have to exemine the offical document for development notes via
One of Most important thing which placed in official document,
  • You have to call it on main thread.
  • Disable it when you do not need because it will drain the battery.
  • This service is using to recognize the audios from remote servers so we need to enable internet permission.

Let's write some code now :)

Place below permissions AndroidManifest.xml file.

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
Now prepare the simple layout. I will add a EditText and ImageButton in the layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:id="@+id/txtRecognizedSpeech" />

        android:src="@android:drawable/ic_btn_speak_now" />

Our layout is ready, and now we are good to write codes. In our MainActivity class first we need to declare requred variables.

Below code block you can see the required variables and components for our task.

    private SpeechRecognizer speechRecognizer;

    private EditText txtRecognizedSpeech;
    private ImageButton btnSpeak;

    private Boolean isListening = false;
    private int RecordAudioRequestCode = 1;
We also check the permission is Granted. Below code block will do this task for us. Do not forget to implement below override code block.

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RecordAudioRequestCode && grantResults.length > 0 ){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
                Toast.makeText(this,"Permission Granted",Toast.LENGTH_LONG).show();
Then, in onCreate method of the activity, first of all we have to check the permission is granted for above API23 devices. Below code block will do this task.

if(ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},RecordAudioRequestCode);
Then initialize the declared variables above:

txtRecognizedSpeech = findViewById(;
btnSpeak = findViewById(;
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
Later we have to initialize the Speech recognition Intent with required configurations. Below configurations we set the language as device default language.

final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
Then, we set the Speech Recognition Listener with below override methods.
In this code block I have removed the unused override methods to prevent confusing. When you implemented this listener make sure all override methods are exist.

        speechRecognizer.setRecognitionListener(new RecognitionListener() {

            public void onBeginningOfSpeech() {
//         ...
//        ....
//        ...
//        ...
            public void onResults(Bundle bundle) {
                ArrayList<String> speechData = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

And lastly we need to define our button click event listener to manage the starting and stopping the audio listener intent.

btnSpeak.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (isListening.equals(true)){
                    isListening = false;
                else if (isListening.equals(false)){
                    isListening = true;
Above code blocks our onCreate method is ready. Now As official document said before we need to destroy this service to prevent the draining the battery. So we need to implement the onDestroy method like below.

    protected void onDestroy() {
Below image you can see the output of the example application. Android Speech to Text Example ScreenShot

That is all in this article.

Burak Hamdi TUFAN.


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.


  • There is no comment. Be the owner of first comment...