Speech to text in android

This tutorial taken from androidhive website.

public class SpeechToTextDemo extends AppCompatActivity {

private final int SPEECH_RECOGNITION_CODE = 1;

private TextView txtOutput;

private ImageButton btnMicrophone;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_speech_to_text_demo);

txtOutput = (TextView) findViewById(R.id.txt_output);

btnMicrophone = (ImageButton) findViewById(R.id.btn_mic);

btnMicrophone.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startSpeechToText();

}

});

}

private void startSpeechToText() {

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,

"Speak something...");

try {

startActivityForResult(intent, SPEECH_RECOGNITION_CODE);

} catch (ActivityNotFoundException a) {

Toast.makeText(getApplicationContext(),

"Sorry! Speech recognition is not supported in this device.",

Toast.LENGTH_SHORT).show();

}

}

/**

* Callback for speech recognition activity

* */

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case SPEECH_RECOGNITION_CODE: {

if (resultCode == RESULT_OK && null != data) {

ArrayList<String> result = data

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

String text = result.get(0);

txtOutput.setText(text);

}

break;

}

}

}

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_speech_to_text_demo"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:background="@color/colorAccent"

tools:context="sciens.com.praticalsdemo.SpeechToTextDemo">

<TextView

android:id="@+id/txt_output"

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/btn_mic"

android:layout_width="80dp"

android:layout_height="80dp"

android:background="@null"

android:scaleType="centerCrop"

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

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="Speech to text using Google API"

android:textColor="@color/white"

android:textSize="15dp"

android:textStyle="normal" />

</LinearLayout>

</RelativeLayout>

add internet permission in manifest file.

Output: Not available.