/Android Speech Recognition

Android Speech Recognition

First you need to create android project then put this XML file in the path:


in this file we will create a button to receive our request to speak and a list view which will list the suggested results for our speech.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:text="Speak" />

        android:layout_height="fill_parent" >


Then, create a class named VoiceReco in your src path on this activity

#see comments in source code blow

First : we have to set up our layout and the list view , set click listener to our button which invoke on click method.

Second: in onclick method we make an intent which will fire speech recognition by passing an action called ACTION_RECOGNIZE_SPEECH

Third : passing some data to our intent such as

  1. Inform the recognizer which speech model to prefer when performing.
  2. Optional text prompt to show the user when asking them to speak.
  3. startActivityForResult

Finally we have to override onActivityResult method which will receive the result of the speech recognizer

  • check if the request sent successfully .

  • receive results in an array list of strings.

  • then show the results in a list view by using Array adapter.

package com.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.view.View.OnClickListener;

public class VoiceReco extends Activity implements OnClickListener{
	ListView lv; 
	int check=121; 
	protected void onCreate(Bundle savedInstanceState) {
		// first
		Button b=(Button)findViewById(R.id.b_speak);
	public void onClick(View v) {
		// second
		Intent in= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 								RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		in.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak");
		startActivityForResult(in, check);

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==check && resultCode == RESULT_OK){
			lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));


Note : this example may not work on your android emulator but it will work fine on your android phone.


Computer science student at faculty of computers and information systems-Mansoura University,Egypt,member@catreloaded Team. python developer Interested in IT field generally and specially open source technologies.