Closed tank777 closed 7 years ago
Here's the code you need. First, check that voice recognition is available on the user's device.
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) {
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
Setup the searchbar and link the voice search item.
mActionVoice = (MenuItem) findViewById(R.id.action_voice);
mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() {
@Override
public void onActionMenuItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_voice)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//... put other settings in the Intent
startActivityForResult(intent, 0);
}
}
Setup onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 0 && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mSearchView.setSearchFocused(true);
mSearchView.setSearchText(results.get(0));
}
super.onActivityResult(requestCode, resultCode, data);
}
That should do it! Hope this helped :+1:
In the current build, cursor is not set at end when voice dictation is finished. PR #209 should fix this problem.
how to implement voice search?