UPDATED:
I am creating an app where onClickListener is used to convert speech to text and input the text to a List View field. Likewise once entered I would like to re-trigger the speech to text option and input the new text into another List View field whilst still retaining all the other List View fields which were already filled in.
The following is part of the Java file that calls the speech to text option with the various statements I want answered. Originally the prompts appeared exactly one after the next which is perfect but it didn't assign each text to the corrresponding edit text field. It only recorded the speech to text of the very last prompt only. Now with some help on this thread I have updated the code to the following, where checklv1 etc correspond to unique integers:
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the current time?");
startActivityForResult(i1, check);
Intent i2 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i2.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i2.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the status Confirmed or Unconfirmed?");
startActivityForResult(i2, checklv1);
Intent i3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i3.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i3.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the Temp?");
startActivityForResult(i3, checklv2);
}
The following part of code shows to assign the text to the editText field lv1, lv2 and lv3:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv1 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv2 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv3.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
}
The code is now progressing and inputting the speech to text phrases into each of the list view fields. However my only issue is that lv3 contains the correct phrase, lv1 also contains the correct phrase but lv2 is a the same phrase as lv1 when it should be the phrase corresponding to lv2. Most likely there is something wrong in the check assignment.
If anyone can please help me with where I have gone wrong in the above code that will be greatly appreciated
The check field you are passing to startActivityForResult is what you should be using to distinguish the action you are performing. Pass 3 different values here and switch on them in onActivityResult to determine which part of the UI you want to update.
startActivityForResult(i2, checkLv1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode) {
case checkLv1:
update(lv1);
...
Related
So I am trying to make a simple to do list app where it has only a mic button and the list. I am very new to android app dev, i have managed to figure out a text input into the list and how to get the text to speech up and put the spoken text in a text field. All this was achieved through a mix of tutorials. I can't seem to figure out how to bring the 2 together.
Any tips?
Here I leave you a great tutorial considering that you didnt post any code.
This tutorial, shows you how to make speech recognition with a button and then it makes a list with the possible spoken text. It works perfectly, I tried once.
Try this code to open the mic on button --> OnClickListener.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
startActivityForResult(intent, nRESULT_SPEECH);
}
nRESULT_SPEECH is your code which you can give anything as 0, 1, 2,etc;
You will get the word spoken in this callback method onActivityResult
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case nRESULT_SPEECH:
if (null != data) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String textCapturedFromVoice=text.get(0);
}
break;
}
}
Once u will get the text in textCapturedFromVoice, you can add this to ur list.
I am trying to have the app recognize certain words said by the user using the code below, but for some reason it isn't working at all. Please review this and tell me what is wrong with it. Thank you
The app is simply suppose to display a toast message if the words "orange" or "apple" is said but nothing happens when using the code below.
//button onclick to trigger RecognizerIntent
public void OnClick_Speed_Detector(View v)
{
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak up");
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 1 && resultCode == RESULT_OK)
{
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(((result).equals("orange")))
{
Toast.makeText(getApplicationContext(), "orange", Toast.LENGTH_LONG).show();
}
else
if (((result).equals("apple")))
{
Toast.makeText(getApplicationContext(), "apple", Toast.LENGTH_LONG).show();
}
}
}
Your issue is that you are testing if an ArrayList == a String, when it can't possibly (an ArrayList of type String contains multiple strings)
Instead of:
if ((result).equals("orange")) {}
Try:
if ((result).contains("orange")) {}
This code will look through every index of the ArrayList and determine if any of the indexes of it equal "orange". If any do then it will return
true
...and it will execute the if statement!
Hope this helps!
In my fragment I have an ExpandableListView, which has Categories as groups and Base_Items as children, they're coming from a DB. From here I open an Intent for result to enter new Items. OnActivityResult I want the new item to appear immediately in the ExpandableListView and then enter it to the DB. I tried that:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
switch(requestCode)
{
case Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM:
String new_item_name = data.getExtras().getString("ITEM_NAME");
int new_item_category = data.getExtras().getInt("ITEM_CATEGORY");
Base_Item bi = new Base_Item(-1, new_item_category, new_item_name);
ArrayList<Base_Item> arr_bi = arr_all_categories.get(new_item_category).getCategory_items();
arr_bi.add(bi);
adapter.notifyDataSetChanged();
manager.add_new_base_item(bi);
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The new item is being inserted into DB just fine, but the ListView doesn't update. I have to exit the app and reopen it to see the new item in the list.
How can I update the ExpandableListView and see the new item immediately? Thank you!
Well, I ended up just repopulating the ListView again in my onActivityResult method. I'm not sure it's the best way (speaking of performance) and perhaps I should've been keep looking for the answer, but for now it just works.
I am having difficulty in framing this question but anyways here it goes, I have made two applications basically the TextToSpeech app and the SpeechToText application. They are working fine, now I am trying to merge them. Basically I want the the text to speech part to say the text entered in a text field , only if the user says the word "speak".That would be done using the speech to text part. Now the problem I am having is that android displays a list of outputs when the user says something, but I only one output that is "Speak" . How can I get only one output instead of a list.
Initially the app becomes active on button click.
The code I am trying is as follows:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "system Activated");
startActivityForResult(i, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == check && resultCode == RESULT_OK) {
String command = data
.getStringExtra(RecognizerIntent.EXTRA_RESULTS).toString();
if (command.equals(command_verify)) {
speak();
} else
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
public void speak() {
String text_to_speak = message_field.getText().toString();
talk.speak(text_to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
The Voice Recognizer returns you a list of guesses, where the first guess is the most accurate. You can use it calling list.get(0). Hope this helps.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
deserializeQuotes();
quotesAdapter.notifyDataSetChanged();
}
}
My array clearly has been updated, and I can see the change when my app starts, but why doesn't it update in this method? The code enters this method.
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
quotesAdapter.notifyDataSetChanged();
listView.setAdapter(quotesAdapter);
Works, but why do I have to create a new adapter? Why can't I use my existing one?