Search code examples
androidtext-to-speech

TextToSpeech - Android


I'm working on a simple game. The idea of the first round is that, there are 26 buttons (images) on the first activity - english alphabet and I want to make it pronounce on button click. For example, when I press "a" button, it must say "a". How can I get the ID of each button, then string of it and give it to speak() method as a parameter? THANKS IN ADVANCE !

Code is here:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

import java.util.Locale;
import java.util.Random;


import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class FirstActivity extends Activity {

    /** Called when the activity is first created. */

    TextToSpeech ttx;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstact);

        ttx = new TextToSpeech(
                FirstActivity.this, 
                new TextToSpeech.OnInitListener() {

            public void onInit(int status) {
                // TODO Auto-generated method stub
                if(status !=TextToSpeech.ERROR){
                    ttx.setLanguage(Locale.US);                 
                }

            }
        });


        TableLayout table = (TableLayout) findViewById(R.id.table);

        String alphabet = "abcdefghijqlmnopqrstuvwxyz";

        int pos = 0;
        for (int i = 0; i < 7; i++) {
            TableRow row = new TableRow(this);
            for (int j = 0; j < 4; j++) {
                if (pos == 26) {
                    break;
                }
                Button but = new Button(this);
                /*but.setOnClickListener(btnListener);*/
                but.setText(Character.toString(alphabet.charAt(pos++)));
                but.setHeight(80);
                but.setWidth(70);
                but.setBackgroundResource(R.drawable.burti);
                row.addView(but);
                but.setId(pos);

                 but.setOnClickListener((OnClickListener) this);


            table.addView(row);

        }}



    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        if(ttx != null){
            ttx.stop();
            ttx.shutdown();     
        }
        super.onPause();
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub


        String pressed = but.getText().toString(); //error here....
        ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null);

    }

}

Solution

  • I made a working example: http://frank.anemaet.nl/android/TalkingButtons.zip Here is a snippet of the working code:

    for (int i = 0; i < alphabet.length(); i++)
        {
            TableRow row = new TableRow(this);
            Button but;
            but = new Button(this);
            but.setText(Character.toString(alphabet.charAt(i)));
            but.setHeight(button_width);
            but.setWidth(button_height);
            but.setId(i);
    
            but.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                  performClick(v);
                }
              });
    
            row.addView(but);
            table.addView(row);
        }
    

    ...

    public void performClick(View arg0){
    
        Button tv = (Button)findViewById(arg0.getId());
        String pressed = tv.getText().toString(); 
        ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null);
    
    }