Deepak Ror
Deepak Ror

Reputation: 333

Speech to Text in my Listview Android

I want to Search in my Listview by Speech to text(STT). I have a list of City ,now when User Search by STT. This Search is apply only on my listview (Like STT Contact Search),

Upvotes: 1

Views: 1816

Answers (2)

stacktry
stacktry

Reputation: 322

Speech to Text is built into Android 1.6+. Here is a simple example of how to do it.

 /**
 * Showing google speech input dialog
 * */
private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case REQ_CODE_SPEECH_INPUT: {
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> result = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            txtSpeechInput.setText(result.get(0));
        }
        break;
    }

    }
}

More info: http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/

Updated:

Try this code for filter view.

http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/

Upvotes: 2

Pratik Butani
Pratik Butani

Reputation: 62411

See this code Google Voice in our Application

/* Google Voice open while calling Activity */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)          
{

    /* Call Activity to Open Google Voice */
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

    try {
        startActivityForResult(intent, 1);
    } catch (ActivityNotFoundException a) {
       Log.d("LOG",a.getMessage());
    }
}

/* This will be called after you speak */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case 1: {
        if (resultCode == Activity.RESULT_OK && null != data) {

            ArrayList text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

           Log.d("LOG","You have speak  : "+text.get(0));
        }
        break;
    }
    }
}

You will get text.get(0) as result of speaked string that you can use in filtration of ListView.

notifyDataSetChanged() is useful to change of ListView with new Data.

Upvotes: 0

Related Questions