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 8288743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:20:19+00:00 2026-06-08T12:20:19+00:00

I have a voice input class that works absolutely great. However, I want the

  • 0

I have a voice input class that works absolutely great. However, I want the user of my app to be able to use voice input code on any page of the app. What I need to do is have a button set up on every xml that allows me to use my voice input code without having to copy all of my voice input code into every single class. How do I reference my code so I can simply have something in my code that says if I press this button it does the activity in another class?
The code in order as follows is, voiceinput code java class, java class that I want to be able to have a button that uses this code.

package com.example.com.proto1;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Sample code that invokes the speech recognition intent API.
 */
public class VoiceRecognition extends Activity implements OnClickListener {



    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

    public ListView mList;
    public Button speakButton;

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle voiceinput) {
        super.onCreate(voiceinput);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.voice_recognition);

        // Get display items for later interaction

        voiceinputbuttons();


        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }

    public void voiceinputbuttons() {
        // TODO Auto-generated method stub
        speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);
    }

    /**
     * Handle the click on the start recognition button.
     */
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }

    /**
     * Fire an intent to start the speech recognition activity.
     */
    public void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

    /**
     * Handle the results from the recognition activity.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
                && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it
            // could have heard
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, matches));
            //matches is the result of voice input. It is a list of what the user possibly said.
            //Using an if statement for the keyword you want to use allows the use of any activity if keywords match
            //it is possible to set up multiple keywords to use the same activity so more than one word will allow the user 
            //to use the activity (makes it so the user doesn't have to memorize words from a list)
            //to use an activity from the voice input information simply use the following format;
            //if (matches.contains("keyword(s) here") {  startActivity(new Intent("name.of.manifest.ACTIVITY")

            if (matches.contains("information")) {
                startActivity(new Intent("android.intent.action.INFOSCREEN"));
            }

            if (matches.contains("home")) {
                startActivity(new Intent("android.intent.action.MENU"));
            }

        }

        super.onActivityResult(requestCode, resultCode, data);

    }
}











package com.example.com.proto1;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.speech.tts.TextToSpeech;
import java.util.ArrayList;
import java.util.List;

public class menu extends Activity implements TextToSpeech.OnInitListener {

    MediaPlayer sep, aep, vpm;

    TextToSpeech mTts;

    public void onInit(int i) {
        // TODO Auto-generated method stub
        mTts.speak("EyePhone Main Menu", TextToSpeech.QUEUE_FLUSH, null);

    }

    @Override
    protected void onCreate(Bundle aboutmenu) {
        // TODO Auto-generated method stub
        super.onCreate(aboutmenu);
        setContentView(R.layout.mainx);


        // Setting up the button references
        Button info = (Button) findViewById(R.id.aboutbutton);
        Button voice = (Button) findViewById(R.id.voicebutton);
        Button speakButton = (Button) findViewById(R.id.btn_speak);

        info.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.INFOSCREEN"));

            }
        });

        voice.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    Intent voiceIntent = new Intent(
                            "android.intent.action.RECOGNITIONMENU");
                    startActivity(voiceIntent);
                } catch (Exception e) {

                }
            }
        });

        speakButton.setOnClickListener(new View.OnClickListener() {

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

                    //this is the place where I thought the code should go that I am asking about

                } catch (Exception e) {

                }
            }

        });

    }

}
  • 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-08T12:20:21+00:00Added an answer on June 8, 2026 at 12:20 pm

    Define a STATIC method in a new class file (lets say TTSHelper) which takes Activity reference as parameter (and others if required). Now call this static method from the onClick method of your button. Doing so will allow you not to repeat the code in each activity class.

    Define as:

    public class TTSHelper {
        public static void myMethod(Activity activity){
            //do whatever you want
        }
    }
    

    Use as:

    public void onClick(View v) {
        TTSHelper.meMethod(this);
    }
    

    P.S. You may replace reference of Activity with Context as a parameter to your static method, but then you should be aware of which context reference you need to pass (activity/application).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm building an app that allows input from the user with their voice.
I have found nice code, that stylize checkboxes. HTML: <input id=checkbox type=checkbox class=checkbox />
I have code that acts properly in FF, Safari and Chrome, however the result
I have to implement a iphone application which will record user's voice as you
I am working on sms application in android in which user have to input
I have two input fields. The main idea is that whenever you focus to
I have created a Server app that receives sound from client, i then broadcast
I have two fields in a form <div class=form> <label class=title>{'webmasterSubmitWebsite_rss_feed_title'|lang}</label> <div class=infos><input type=text
I have this Jquery: $(document).ready(function() { $('#masterChecks input[type=checkbox]').click(function() { var togClass=$(this).attr('class'); if($(this).attr('checked')){ //need to
In my script i created a file that i want to use as an

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.