Beruflich Dokumente
Kultur Dokumente
HOME
SPEECH INPUT
Tips
Write for Us
SEARCH HERE
WERE SOCIAL
AndroidHive
37,176 likes
Like Page
Contact Us
Subscribe to Newsletter
DOWNLOAD CODE
I have created a simple app to demonstrate this tutorial. Below is the screenshot of the app
which contains a simple button to invoke speech input and a TextView to display the converted
speech text.
Sign Up
We strictly care about your
privacy!
Sample Application
1. Create a new project in Eclipse by going to File New Android Application Project and
give required information.
2. Open strings.xml located under res values and add below string values.
strings.xml
</resources>
3. Open colors.xml located under res values and add below colors. If you dont see
colors.xml, create a new file and add the values.
colors.xml
4. Now open the layout file for main activity(activity_main.xml) and add below code to create a
simple layout.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"
android:orientation="vertical" >
<TextView
android:id="@+id/txtSpeechInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textColor="@color/white"
android:textSize="26dp"
android:textStyle="normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/ico_mic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tap_on_mic"
android:textColor="@color/white"
android:textSize="15dp"
android:textStyle="normal" />
</LinearLayout>
</RelativeLayout>
5. Finally open your MainActivity.java and do the following changes. In simple adding speech
input will be done in two steps.
MainActivity.java
package info.androidhive.speechtotext;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptSpeechInput();
}
});
/**
/**
* 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();
}
}
/**
* Receiving speech input
* */
@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) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Run the app in a real device. Make sure that the device has good internet connectivity while you
are testing.
SUBSCRIBE
Ravi Tamada
Ravi is hardcore Android programmer and Android programming has been his
passion since he compiled his first hello-world program. Solving real problems
of Android developers through tutorials has always been interesting part for
him.
Share
Login
Sort by Newest
Sokhorn Houn
12 days ago
how can I get recorded voice and text at the same time?
Reply
Wiam
17 days ago
Lonr
Reply
Lonr
19 days ago
Question: How can i implement voice trigger input just like, "ok google" in Android's google app?
Reply
shivam
21 days ago
hi Ravi i need your help in developing some android apps i want to interconnect two apps for
sharing some details or data using mobile network not in WiFi or Bluetooth but i can't understand
how to implement can you help me for implementation.............
Reply
Reply
Adam
a month ago
thank you Mr. Ravi Tamada for this tutorial. it's helpful code,
I have one question: how I can start voice recognition directly when button clicked (without using
Intent).
do you have any Idea how I can do this?
thanks
Reply
brahmy
2 months ago
voice search miss understanding problem i am facing sir.suppose if i wana speaking R then it is
voice search miss understanding problem i am facing sir.suppose if i wana speaking R then it is
understanding like "are" and then it's displaying like this..how resolve this problem.
Reply
Arifin Firdaus
2 months ago
Hi Ravi, How can I input voice with another language other than english? Can anyone tell me???
Reply
i found it (change the primary input voice) in settings -> Language & input -> Voice input(under
the speech section).
Reply
Mangesh Panchwagh
2 months ago
hey
I have a question When spoken words are converted into text, how to store it
into a text file. So that I can use it(text file) later.
Reply
Hi Mangesh,
I've not tried it myself but I'm pretty sure this would work. Get the text from the textView and save
it in a file. http://developer.android.com/t...
Reply
Nikhil
2 months ago
Hi Ravi, I tried implementing offline support as you have indicated. I downloaded the packages, but
still offline support is not working for my app. The app works perfectly when its online.... Any
suggestions?
Reply
Mirko Pontoriero
3 months ago
Hi, I would like to know why this App not work offline? I have a NEXUS 5 and I following the code
also the screenshot example but not work. I hope that someone answer me early. Thanks
Reply
Speech to Text needs internet. When we give speech to app, it connect to google services and
translates audio to text.
Reply
azad
5 months ago
Reply
Gnana poobalan
5 months ago
Does it work on Android 5 with offline mode in micromax android one device?. I need to know about
Does it work on Android 5 with offline mode in micromax android one device?. I need to know about
it. Any Suggestions would be appreciated..
Thanks in advance,
-poobalan
Reply
karuna thevar
6 months ago
" your device doesnt support speech to text ", I am getting this error
Reply
Larcio Metzner
6 months ago
You did a great Job! I'm looking for some how to get just Letters and Numbers, not full words nor
sentences. My users are going to enter information just like 'AA001', 'BC022', 'AD011', and I still had
not figure out how to get it.
Reply
Yoshua Rudy
6 months ago
Hi, Ravi I already press the subscribe button but I didn't receive any email regarding confirmation of
my subscribtion
Reply
santosh keshari
9 months ago
Hi Ravi, google voice recognition engine does not recognize medical terms. I need to implement app
to detect medical terms. Which engine shall i use? Do you have any ides?
Reply
Reply
Subscribe
Privacy
QUICK CONTACT
ABOUT ANDROIDHIVE
Advertise with us
Privacy Policy
Terms of Service
Sitemap
advanced topics.