---
title: "Speech to Text example in Android"  
description: "In this article I am explaining about Speech to Text example in Android"  
author: "Manoj Pandey"  
published: 2015-03-10  
updated: 2020-07-11  
canonical: https://www.mindstick.com/articles/1643/speech-to-text-example-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Speech to Text example in Android

Synthesizes speech from text for [immediate](https://answers.mindstick.com/qa/96807/tadarise-immediate-informal-solution-of-ed-in-men) playback or to create a sound file. Text To Speech example in [Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android)\

Apps with [interfaces](https://www.mindstick.com/articles/26/interfaces) that make use of voice have a [unique](https://yourviews.mindstick.com/view/81599/how-to-change-yourself-follow-these-4-unique-life-hacks-for-good) appeal. They tend to make their users feel that they are using something futuristic. Since its early days, Android has had very robust text-to-speech (TTS) functionality.

Android allows you [convert](https://www.mindstick.com/blog/698/convert-text-document-to-pdf-file) your text into voice. Not only you can convert it but it also allows you to speak text in variety of different languages.

Here I am creating a sample for [speech to text](https://www.mindstick.com/forum/23319/speech-to-text-generation). In this [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) which will you speak then it prints in text.

1. Create an Android [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful)

2. Add shape for [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) stylist name as bg_gradient.xml in drawable folder

\

```
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >     <gradient        android:angle="45"        android:endColor="@color/bg_gradient_end"        android:startColor="@color/bg_gradient_start"        android:type="linear" /></shape>
```

3. Add icon name as ico_mic.png.

\
![Speech to Text example in Android](https://www.mindstick.com/mindstickarticle/739b7670-b830-4736-9d63-f097aa1e9122/images/5eceb209-7249-453e-a258-4d68c9c6ff29.png)

4. My String recourse

```
<?xml version="1.0" encoding="utf-8"?><resources>     <string name="app_name">Speech To Text</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="speech_prompt">Say something&#8230;</string>    <string name="speech_not_supported">Sorry! Your device doesn\'tsupport speech input</string>    <string name="tap_on_mic">Tap on mic to speak</string> </resources>
```

\

5. My activity_main.xml file

```
<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:background="@drawable/bg_gradient"    android:orientation="vertical" >     <TextView        android:id="@+id/txtSpeechInput"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="100dp"        android:textColor="@color/white"        android:textSize="26dp"        android:textStyle="normal" />     <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="60dp"        android:gravity="center"        android:orientation="vertical" >         <ImageButton            android:id="@+id/btnSpeak"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@null"            android:src="@drawable/ico_mic" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="@string/tap_on_mic"            android:textColor="@color/white"            android:textSize="15dp"            android:textStyle="normal" />    </LinearLayout> </RelativeLayout>
```

6. Add following code in ManiActivity.class

```
package info.androidhive.speechtotext; import java.util.ArrayList;import java.util.Locale; import android.app.Activity;import android.content.ActivityNotFoundException;import android.content.Intent;import android.os.Bundle;import android.speech.RecognizerIntent;import android.view.Menu;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends Activity {                 private TextView txtSpeechInput;                private ImageButton btnSpeak;                private final int REQ_CODE_SPEECH_INPUT = 100;                 @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                                 txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);                                btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);                                 // hide the action bar                                getActionBar().hide();                                btnSpeak.setOnClickListener(new View.OnClickListener() {                                                @Override                                                public void onClick(View v) {                                                                promptSpeechInput();                                                }                                });                }                /**                 * Showing google speech input dialog                 * */                private void promptSpeechInput() {                                // RecognizerIntent constants for supporting speech recognition through starting an                                // Intent                                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);                                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,                                                                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);                                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());                                intent.putExtra(RecognizerIntent.EXTRA_PROMPT,                                                                getString(R.string.speech_prompt));                                try {                                                startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);                                } catch (ActivityNotFoundException a) {                                                Toast.makeText(getApplicationContext(),                                                                                getString(R.string.speech_not_supported),                                                                                Toast.LENGTH_SHORT).show();                                }                }                /**                 * Receiving speech input                 * */                @Override                protected void onActivityResult(int requestCode, int resultCode, Intent data) {                                super.onActivityResult(requestCode, resultCode, data);                                 switch (requestCode) {                                case REQ_CODE_SPEECH_INPUT: {                                                if (resultCode == RESULT_OK && null != data) {                                                                 ArrayList<String> result = data                                                                                  .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);    txtSpeechInput.setText(result.get(0));                                                }                                                break;                                }                                }                }} 
```

Now run your application

![Speech to Text example in Android](https://www.mindstick.com/mindstickarticle/739b7670-b830-4736-9d63-f097aa1e9122/images/2ddcca5c-b63e-4858-847f-8efe8f74cc70.png)

![Speech to Text example in Android](https://www.mindstick.com/mindstickarticle/739b7670-b830-4736-9d63-f097aa1e9122/images/262f2718-42bf-43f6-8992-181b7596aff1.png)

Next article is -: Text to Speech in Android

---

Original Source: https://www.mindstick.com/articles/1643/speech-to-text-example-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
