About
Home > Tutorials Home > Android > Speech To Text Example in Android

Speech To Text Example in Android

Published on October 17, 2019 8:25:59 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Speech to Text in Android".

<LinearLayout 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:orientation="vertical">
    <ImageButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_btn_speak_now"
        android:id="@+id/btnspeck" /> 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview" />
</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.speechtotext;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ImageButton btnspeack;
	private TextView textview;
	protected static final int RESULT_SPEECH = 1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview=(TextView)findViewById(R.id.textview);
        btnspeack=(ImageButton)findViewById(R.id.btnspeck);

        btnspeack.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v)
			{
				Intent ii=new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				ii.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en-US");

				 try
				 {
	                    startActivityForResult(ii,RESULT_SPEECH);
	                    textview.setText("");
	             }
				 catch (ActivityNotFoundException a)
				 {
	                    Toast.makeText(getApplicationContext(),"Device not support Speech to Text",Toast.LENGTH_SHORT).show();
	             }
			}
		});
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode)
		{
	        case RESULT_SPEECH:
    	    {
        	    if (resultCode == RESULT_OK && null != data)
            	{ 
	                ArrayList text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
	                textview.setText(text.get(0));
    	        }
        	    break;
	        }
		}
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile