Using android text to speech – step by step

Using android text to speech – step by step

 

In this tutorial you will learn how to use the android text to speech library! It is extremely useful when you are making applications for people with reading difficulties, or when your application is required to read an email, a text message, or a text.

The good thing is that google is taking care of everything – you just need to include the library – pass some parameters and viola! The phone is speaking!

For the sake of this tutorial we will need a ListView, an adapter and a click listener – so when an list item is clicked the phone will start reading the text.

This tutorial assumes that you have a basic android and java knowledge.

First thing first we will need a ListView in order to load some data into it:

[code language=”java”]
ListView lv = (ListView)rootView.findViewById(R.id.list); // Declaring our list view
[/code]

The above ListView looks like this in the activity file:

[code language=”xml”]
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight=".50"/>
[/code]

Now we have a list view, but it is empty. We need to fill it with some data(texts) that the device will start reading. The task is pretty easy, once your arrayList is full of data you can assign it to an adapter by the following code(if you’re in a fragment):

[code language=”java”]
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, yourAdapter);// where yourArrayList is your arrayList
[/code]

 

 

And then you simply assign the adapter to the listView:

[code language=”java”]
lv.setAdapter(adapter);
[/code]

 

Easy? Now we are going to give a voice to our application, but how?

Again the answer is – Really easy, but wait, it is a step by step tutorial, so what is the next step? Oh yes – we need to attach a click listener to our listView:

[code language=”java”]

// Using android text to speech – step by step
TextToSpeech tts; // an extra variable

tts=new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.UK);
}
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tts.speak(description + " " + yourArrayList.get(position), TextToSpeech.QUEUE_FLUSH, null); // where yourArrayList is your arrayList
}
});

// Using android text to speech – step by step
[/code]

 

As you can see we have created an extra variable “tts” and we have set the english as a default language – so it will try to read english texts – you always can use one of the supported languages, just by changing the 2 letters.

Now, if you run your application your device will start reading the text of the clicked item!

Congratulations now you know how to implement android text to speech to your android application!

 

 

Using android text to speech – step by step!

Leave a Reply