When displaying the Text To Speech result in the TextView field, the recognized word or phrase appears in square brackets, how to get rid of them?

public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button recognizeButton = (Button)findViewById(R.id.button1); recognizeButton.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView) findViewById(R.id.textView1); speechText.setText(results.toString()); } } 

}

    1 answer 1

    In this line

     results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

    you get an array, then

     speechText.setText(results.toString()); 

    you cast an array to a string. Hence the brackets. Go through all the elements of the array

     results[i].toString() 

    where i is a counter from 0 to results.size ()

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView) findViewById(R.id.textView1); String str=""; for(int i=0;i<results.size();i++){ str+=results.get(i) + " "; } speechText.setText(str); } } 
    • and how to implement it? - Nikolai
    • @ Nikolai, added - Vitalii Obideiko