Open In App
Related Articles

How to Convert Speech to Text in Android?

Like Article
Save Article
Report issue

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 like Google Search also in apps like google keyboard, etc because it gives users a great experience. 


Step 1: Add the below code in activity_main.xml. Here an image view for the mic icon and a textview to show the text that is converted from the speech is added.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintTop_toBottomOf="@+id/iv_mic" />

Step 2: Add the below code in Here onClickListener is added with the mic icon so when the user clicks on the icon(image) of mic it is invoked.  RecognizerIntent.ACTION_RECOGNIZE_SPEECH is used in the listener that starts an activity that prompts the user for speech and send it through a speech recognizer. The results will be returned via activity results in the onActivityResult() method, when the intent is started using startActivityForResult(). In onActivityResult() method a list of strings is returned and the text is replaced with it in the textview.

class MainActivity extends AppCompatActivity(){
    private ImageView iv_mic;
    private TextView tv_Speech_to_text;
    private static final int REQUEST_CODE_SPEECH_INPUT = 1;
      protected void onCreate(Bundle savedInstanceState){
        iv_mic = findViewById(;
        tv_Speech_to_text = findViewById(;
        iv_mic.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
                Intent intent
                    = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text");
                try {
                    startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
                catch (Exception e) {
                        .makeText(MainActivity.this, " " + e.getMessage(),
    protected void onActivityResult(int requestCode, int resultCode,
                                    @Nullable Intent data)
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
            if (resultCode == RESULT_OK && data != null) {
                ArrayList<String> result = data.getStringArrayListExtra(



Last Updated : 28 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads