Question

I know there is like a ton topic and question regarding Voice Recognition and my question might be a stupid one too. but please bear with me guys.

I need to get the result of the speech recognition into an (Editable Text Box) instead of (Array List), the editable text box to allow the user to edit the result , just like a memo.

I found some questions like mine but I could not understand ,I am still a beginner comparing to you guys .

This is the code :

public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

private ListView mlvTextMatches;
private Button mbtSpeak;
private Button reButton;
private EditText result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
         Toast.LENGTH_LONG).show();

result = (EditText) findViewById(R.id.out_text); 
mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){


    public void onClick(View v) {

        startActivity(new Intent(v.getContext(),KeyBoard.class));
    } 
 });


checkVoiceRecognition();
}

public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
 Toast.LENGTH_SHORT).show();

 }
 }

public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}          


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
     // populate the Matches
     mlvTextMatches .setAdapter(new ArrayAdapter<String>      
(this,android.R.layout.simple_list_item_1,textMatchList));
 }

 }


 //Result code for various error.
{
 } if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
 showToastMessage("Audio Error");
}else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
 showToastMessage("Client Error");
}else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
showToastMessage("Network Error");
}else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
showToastMessage("No Match");
}else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
showToastMessage("Server Error");
}
 super.onActivityResult(requestCode, resultCode, data);
 }
/**
  * Helper method to show the toast message
  **/
  void showToastMessage(String message){
  Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   }
 }

This is the code after editing :

public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

private Button mbtSpeak;
private Button reButton;
private EditText myEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
         Toast.LENGTH_LONG).show();

myEditText = (EditText) findViewById(R.id.out_text); 
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){


    public void onClick(View v) {

        startActivity(new Intent(v.getContext(),KeyBoard.class));
    } 
 });


checkVoiceRecognition();
}

public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
 Toast.LENGTH_SHORT).show();

 }
 }

public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}          


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
    // populate the Matches
    myEditText.setText(textMatchList.toString());
    // if the above does not look good
    // for (String match : textMatchList) {
    //        myEditText.append(match + "\n"); // or whatever separator you want
        //   }
     }

}

the second try is :

 } else {
    // populate the Matches
    //myEditText.setText(textMatchList.toString());
    // if the above does not look good
    for (String match : textMatchList) {
           myEditText.append(match + "\n"); // or whatever separator you want
           }
     }

}
Was it helpful?

Solution

    if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
     // populate the Matches
    result.setText(textMatchList.toString());
    // if the above does not look good
    // for (String match : textMatchList) {
    //        result.append(match + "\n"); // or whatever separator you want
    //   }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top