Browse through more Android tutorials. If you'd like to see a tutorial on any particular topic, do leave a comment in the wishlist page. We frequently post new tutorials along with app releases. You may subscribe to our newsletter to get all updates in your inbox.
Now you can get the latest Java source bundled with each app update. Install the app from Google Play and go to Settings > Extras.

«  Create a notepad/to-do list app Create an instant messaging app  »

Create an App Widget in Android with Text-to-Speech (TTS)


Keywords: AppWidgetProvider RemoteViews AppWidgetManager BroadcastReceiver Widget Configuration Activity AlarmManager TextToSpeech Service PreferenceActivity OnPreferenceChangeListener


4 « Prev Page

10. Android Text-to-Speech (TTS) using Service

Text-to-Speech functionality enables an Android device to "speak" with help of TTS engine and language specific data. Android provides an easy way of querying the platform for availability of these language files using an Intent.

	private static final int TTS_CHECK_CODE = 101;

	protected void onCreate(Bundle savedInstanceState) {

		//... usual stuff
		Intent checkIntent = new Intent();
		startActivityForResult(checkIntent, TTS_CHECK_CODE);		

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == TTS_CHECK_CODE) {
	        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
	            // success, create the TTS instance
	        } else {
	            // missing data, install it
	            Intent installIntent = new Intent();
If TTS data is missing then in onActivityResult callback we launch an activity that directs user to the Play Store for downloading and installing the data.
If data is already present then we can create the TTS instance and configure it.

You can read more about using TTS from Android developers blogspot.

Since we are going to use a Service for Text-to-Speech implementation so we will initialize TTS inside the service.
First, register the service in manifest.
        <service android:name=".SpeechService" >
Next, create SpeechService class as follows.
	public class SpeechService extends Service implements TextToSpeech.OnInitListener {
		public static final String EXTRA_WORD = "word";
		public static final String EXTRA_MEANING = "meaning";
		private TextToSpeech tts;
		private String word, meaning;
		private boolean isInit;
		private Handler handler;
		public void onCreate() {
			tts = new TextToSpeech(getApplicationContext(), this);
			handler = new Handler();

		public int onStartCommand(Intent intent, int flags, int startId) {
			word = intent.getStringExtra(SpeechService.EXTRA_WORD);
			meaning = intent.getStringExtra(SpeechService.EXTRA_MEANING);
			if (isInit) {
			handler.postDelayed(new Runnable() {
				public void run() {
			}, 15*1000);
			return SpeechService.START_NOT_STICKY;

		public void onDestroy() {
			if (tts != null) {

		public void onInit(int status) {
			if (status == TextToSpeech.SUCCESS) {
				int result = tts.setLanguage(Locale.US);
				if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
					isInit = true;
		private void speak() {
			if (tts != null) {
				tts.speak(word, TextToSpeech.QUEUE_FLUSH, null);
				tts.speak(meaning, TextToSpeech.QUEUE_ADD, null);

		public IBinder onBind(Intent arg0) {
			return null;

TextToSpeech constructor requires a listener which would receive callback once TTS is ready. So we make SpeechService implement OnInitListener interface and override onInit() method where we configure TTS (e.g. set language) and use it to speak.
Finally, we stop speech service after some time by using a Handler. It is good practice to shut down TTS in onDestroy() method to release resources held by TTS engine.

Now that our speech service is ready, we'll integrate it with our widget.
Modify VocabWidget class as follows.
	public static final String ACTION_SPEAKER = "com.appsrox.dailyvocab.action.SPEAKER";
	static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

		// Construct the RemoteViews object
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.vocab_widget);
		views.setTextViewText(, word);
		views.setTextViewText(, meaning);
		boolean isTTS = prefs.getBoolean(SettingsActivity.TTS_PREF, false);
		if (isTTS) {
			views.setOnClickPendingIntent(, getPendingSelfIntent(context, ACTION_SPEAKER, word, meaning));
			views.setViewVisibility(, View.VISIBLE);
		} else {
			views.setViewVisibility(, View.GONE);
    private static PendingIntent getPendingSelfIntent(Context context, String action, String... content) {
        Intent intent = new Intent(context, VocabWidget.class);
        if (content != null && content.length == 2) {
        	intent.putExtra(SpeechService.EXTRA_WORD, content[0]);
        	intent.putExtra(SpeechService.EXTRA_MEANING, content[1]);
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    public void onReceive(Context context, Intent intent) {
        if (ACTION_SPEAKER.equals(intent.getAction())) {
        	Intent speechIntent = new Intent(context, SpeechService.class);
        	speechIntent.putExtra(SpeechService.EXTRA_WORD, intent.getStringExtra(SpeechService.EXTRA_WORD));
        	speechIntent.putExtra(SpeechService.EXTRA_MEANING, intent.getStringExtra(SpeechService.EXTRA_MEANING));
        } else if (ACTION_UPDATE.equals(intent.getAction())) {
        } else super.onReceive(context, intent);
We check whether TTS is enabled or not before displaying speaker button on the widget. Clicking this button by user results in starting our SpeechService with the text passed as extras. The service gets automatically stopped as discussed before.
Share the love:  

Next Page » 4

App Gen
App Name:
Project Name:
List  Grid  Pager
Action Bar
Navigation Drawer
Dummy Data
Free Apps