Question:
There is an EditText
component. I would like to translate the keyboard into voice input by some gesture. Is this possible?
Answer:
You need to handle your gesture and call the external intent:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
theoretically, it may not be in the system, so first you need to check its presence
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) //нет распознавателя
//blah-blah
If there is a call to the Activity
recognizer via startActivityForResult(intent, SPEECH_REQUEST)
and in onActivityResult()
put the received data in the EditText
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST && resultCode == RESULT_OK) {
//массив распознанных строк
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
//ложим в EditText
}
super.onActivityResult(requestCode, resultCode, data);
}
Update
Actually the answer to the question itself:
translate the keyboard into voice input. Is this possible?
Alas, this is impossible. There is no such INPUT_TYPE
, every keyboard implementation implements voice input support itself.