Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8298297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:43:16+00:00 2026-06-08T15:43:16+00:00

I just completed the coding to have the text to speech button read the

  • 0

I just completed the coding to have the text to speech button read the text on the button the user pressed.For some reason every button says the text on the first button instead of their own text. Obviously this is a problem because you don’t want every button saying the same thing. It registers no errors in my LogCat so it works fine, just not the way I want it to. I don’t have the java experience to find the source of the problem.

public class menu extends Activity implements TextToSpeech.OnInitListener,
        OnClickListener {

    TextToSpeech mTts;
    Button speakButton, infoButton, voiceButton;

    // TTS object
    public TextToSpeech myTTS;
    // status check code
    public int MY_DATA_CHECK_CODE = 0;

    @Override
    protected void onCreate(Bundle aboutmenu) {
        super.onCreate(aboutmenu);
        setContentView(R.layout.mainx);

        SpeakingAndroid speak = new SpeakingAndroid();

        VoiceRecognition voiceinput = new VoiceRecognition();

        // get a reference to the button element listed in the XML layout
        speakButton = (Button) findViewById(R.id.btn_speak);
        infoButton = (Button) findViewById(R.id.aboutbutton);
        voiceButton = (Button) findViewById(R.id.voicebutton);

        // listen for clicks
        infoButton.setOnClickListener(this);
        speakButton.setOnClickListener(this);


        // check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);



        voiceButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

            }
        });

    }
    public void onClick1(View about) {

        // get the text entered
        infoButton = (Button) findViewById(R.id.aboutbutton);
        String words = infoButton.getText().toString();
        speakWords(words);
        Intent infoIntent = new Intent("android.intent.action.INFOSCREEN");
        startActivity(infoIntent);

    }
    // respond to button clicks
    public void onClick(View v) {

        // get the text entered
        speakButton = (Button) findViewById(R.id.btn_speak);
        String words = speakButton.getText().toString();
        speakWords(words);
        Intent voiceIntent = new Intent("android.intent.action.RECOGNITIONMENU");
        startActivity(voiceIntent);

    }



    // speak the user text
    public void speakWords(String speech) {

        // speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }

    // act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    // setup TTS
    public void onInit(int initStatus) {

        // check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:43:17+00:00Added an answer on June 8, 2026 at 3:43 pm

    Declare a text to speech attribute in your activity

    private TextToSpeech    mTTS;
    

    Instantiate your Text to speech Object in your Activity

    mTTS=new TextToSpeech(this,this);
    

    Get a reference to your buttons listed in the XML layout

    speakButton = (Button) findViewById(R.id.btn_speak);
    infoButton = (Button) findViewById(R.id.btn_about);
    voiceButton = (Button) findViewById(R.id.btn_voice);
    

    Listen to ClickEvents for your Button, your activity must implement the View.OnClickListener interface

    infoButton.setOnClickListener(this);
    speakButton.setOnClickListener(this);
    voiceButton.setOnClickListener(this);
    

    Handle Click Events in th Overriden onClick() method:

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.btn_speak:
                mTTS.speak(speakButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            break;
            case R.id.btn_about:
                mTTS.speak(infoButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            break;
            case R.id.btn_voice:
                mTTS.speak(voiceButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            break;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have just completed implementing a successful but problematic project. We want to get
I have just completed an application for my final year project and I need
I have started coding a packet injector and read up a bit on what
I've just completed my second OOP class, and both my first and second classes
I have successfully completed, and no problems with the program, everything works just fine,
I just completed the ios tutorial, and while I can manipulate a single screen
i just completed a web based chat application based on ajax/php. But the problem
I've just completed an automation script that: downloads a project build to local storage
Not that level of failure indeed. I just completed the 4 part tutorial from
I am new in this community. I just completed my Btech in Computer science,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.