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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:12:37+00:00 2026-05-30T08:12:37+00:00

I’m creating a soundboard. When I click on a button, the soundfile is played

  • 0

I’m creating a soundboard.
When I click on a button, the soundfile is played and selectedSoundId is defined with soundIds[i];

If I long press the button, before button is “short” pressed, the selectedSoundId is not defined.
So I need to define it, even if I didn’t “shortpress” the button.

I figured out I’ve to define it after “onCreateContextMenu“, but how?

public class Activity2 extends TabActivity {

    /** Called when the activity is first created. */
    int selectedSoundId;
    int random = (int)Math.ceil(Math.random()*100);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);  

        //Look up the AdView as a resource and load a request.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("text1").setContent(R.id.tab1));
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("text2").setContent(R.id.tab2));
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("text3").setContent(R.id.tab3));
        mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("text4").setContent(R.id.tab4));

        mTabHost.setCurrentTab(0);

        final MediaPlayer player = new MediaPlayer();
        final Resources res = getResources();

        //just keep them in the same order,
        final int[] buttonIds = { R.id.button1, R.id.button2,};
        final int[] soundIds = { R.raw.sound1, R.raw.sound2, };

        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                //find the index that matches the button's ID, and then reset
                //the MediaPlayer instance, set the data source to the corresponding
                //sound effect, prepare it, and start it playing.
                for(int i = 0; i < buttonIds.length; i++) {
                    if(v.getId() == buttonIds[i]) {
                        selectedSoundId = soundIds[i];
                        AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
                        player.reset();
                        try {
                            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            player.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        player.start();

                        break;
                    }
                }
            }
        };
      //set the same listener for every button ID, no need
        //to keep a reference to every button
        for(int i = 0; i < buttonIds.length; i++) {
            Button soundButton = (Button)findViewById(buttonIds[i]);
            registerForContextMenu(soundButton);
            soundButton.setOnClickListener(listener);

        }



    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Save as...");
     menu.add(0, v.getId(), 0, "Ringtone");
     menu.add(0, v.getId(), 0, "Notification");

// How do I give SelectedsoundId here the value of soundIds[i]
 //Like : selectedSoundId = soundIds[i];

    }

UPDATE

I added this code in the OnCreateContextMenu and I think it’s working correctly now.
I’m not sure if v.getId(); is necessary?

 v.getId();
     for(int i = 0; i < buttonIds.length; i++) {
           if(v.getId() == buttonIds[i]) {
               selectedSoundId = soundIds[i];
           }
       }
  • 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-05-30T08:12:39+00:00Added an answer on May 30, 2026 at 8:12 am

    The onCreateContextMenu() method has a parameter named View v that represent the View for which the ContextMenu is being built. So from this you could get the ID (v.getId()) and then, like you did in the View.OnClickListener listener, find out the position in the buttonIds array and then set the selectedSoundId accordingly.

    Also this:

    menu.add(0, v.getId(), 0, "Ringtone");
    

    should be something like this:

    menu.add(Menu.NONE, UNIQUE_ID, Menu.NONE, "Ringtone");
    

    Edit:

    In the code you added i see the line v.getId(); before your for loop, that isn’t necessary.
    Regarding my menu edit you should assign a unique ID for your menu item so in the method onContextItemSelected() you can find out which menu item the user pick(see this class as an example). In the other fields you could use 0 or Menu.NONE.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.