Search code examples
javaandroidspeech-to-text

Speech to text app replaces old text with new


The app overwrites the old text with the new one but that's not the purpose. I want the new text to retain old text in addition to new, like turning old and new into strings and then combining them?

final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {

    @Override
    public void onResults(Bundle bundle) {
        //getting all the matches
        ArrayList<String> matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        //displaying the first match
        if (matches != null)
            resultText.setText(matches.get(0));
    }

});

Solution

  • So you want to append the results to the TextView ?

    That's easy:

    resultText.append(matches.get(0));
    

    With space:

    resultText.append(" " + matches.get(0));
    

    Another way:

    resultText.setText(resultText.getText() + " " + matches.get(0));