I am using the speech to text concept using speech recognizer, but when the text is set to the edit text, the existing text in the edit text is erased and the text converted from the speech is set in its place. But, I want the converted text to be set after the existing text, without replacing the existing text.
public class AddNoteActivity extends AppCompatActivity {
public static final int RECORDAUDIOREQUESTCODE = 1;
private SpeechRecognizer speechRecognizer;
ImageView micButton;
public static final String EXTRA_ID = "EXTRA_ID";
public static final String EXTRA_TITLE =
"EXTRA_TITLE";
public static final String EXTRA_DESCRIPTION =
"EXTRA_DESCRIPTION";
private EditText editTextTitle;
private EditText editTextDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)!=
PackageManager.PERMISSION_GRANTED){
checkPermission();
}
editTextTitle = findViewById(R.id.edit_text_title);
editTextDescription = findViewById(R.id.edit_text_description);
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_baseline_close_24);
micButton = findViewById(R.id.mic);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
micButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
micButton.setImageResource(R.drawable.ic_baseline_mic_24);
speechRecognizer.startListening(speechRecognizerIntent);
}
});
@Override
protected void onDestroy(){
super.onDestroy();
speechRecognizer.destroy();
}
private void checkPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},
RECORDAUDIOREQUESTCODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if(requestCode==RECORDAUDIOREQUESTCODE && grantResults.length>0){
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
Toast.makeText(this, "Permission Granted", Toast.LENGTH_LONG).show();
}
}
CodePudding user response:
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
You're telling it to hilight the entire text, then you set the text. That's going to get rid of whatever the selection is (it's also redundant, as the same thing would happen if you didn't set the selection). If you want to continue it, use
editTextDescription.setText(editTextDescription.getText().toString() data.get(0))
That should replace the last 2 lines of that function. It will get the existing text, tack the new result to it, and set the total string to the view.