package com.example.activity;
import java.util.ArrayList;
import com.example.speechtotext.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SpeechActivity extends Activity {
TextView displayText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.speech_layout);
displayText = (TextView) findViewById(R.id.displayTextView);
Button speakButton = (Button) findViewById(R.id.speakButton);
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(in, 1);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(
getApplicationContext(),
"sorry, your device doesn't support speech to text feature",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
displayText.setText(text.get(0));
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}