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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:26:19+00:00 2026-05-23T11:26:19+00:00

So I’m close with my code. I’ve been trying to get my Soundboard to

  • 0

So I’m close with my code. I’ve been trying to get my Soundboard to utilize Set As Ringtone for two days now. It’s obvious I need to utilize Arrays.

Three sections of code, but not quite sure how to implement it.

1: The array decleration:

int [] buttonArray = {R.id.button1, R.raw.button2};

2: The for statement (WRONG – This is what I’m not sure about)

for (int i=0; i<buttonArray.length; i++){  
      buttonArray[i];  
      }

3: The button switch:(WRONG – I know this is wrong too)

public void onClick(View v) {

    switch (v.getId()) {
    case buttonArray[i]:

@Ted – Ok, below is my full code. The Soundboard portion works great, it’s the Set as Ringtone that’s not working. And I know why. I am using R.raw.ifightsong for the function. I figured I need an array there so it differentiate between the sounds. Not to mention I am going to have 10 buttons and so I”m going to have A LOT of code. I wanted to eliminate A LOT of code by using an array.

package com.hawkaddictsoundboard;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HawkeyeAddictSoundboardActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button fightsong;
    Button oniowa;

    MediaPlayer mMediaPlayer;

    int[] soundArray = {R.raw.ifightsong,R.raw.oniowa}; // etc...


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


        fightsong = (Button) findViewById(R.id.fightsong_button);
        fightsong.setOnClickListener(this);
        oniowa = (Button) findViewById(R.id.oniowa_button);
        oniowa.setOnClickListener(this);
        registerForContextMenu(fightsong);  
        registerForContextMenu(oniowa); 
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.fightsong_button:
               if (mMediaPlayer!=null && mMediaPlayer.isPlaying()) {
                   mMediaPlayer.stop();
                   mMediaPlayer.reset();
                   mMediaPlayer.start();

                    }
               else{
                    if (mMediaPlayer!=null){
                        mMediaPlayer.reset();
                        mMediaPlayer.release();
                }
                    mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.ifightsong);
                    mMediaPlayer.start();
        // TODO Auto-generated method stub
         }
               break;
        case R.id.oniowa_button:
               if (mMediaPlayer!=null && mMediaPlayer.isPlaying()) {
                   mMediaPlayer.stop();
                   mMediaPlayer.reset();
                   mMediaPlayer.start();

               }
               else{
                    if (mMediaPlayer!=null){
                        mMediaPlayer.reset();
                        mMediaPlayer.release();
                }
                    mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.oniowa);
                    mMediaPlayer.start();
        // TODO Auto-generated method stub
         }
               break;
    }}
     @Override
     public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Set As...");
     menu.add(0, v.getId(), 0, "Ringtone");
     menu.add(0, v.getId(), 0, "Notification");
     }

     @Override  
     public boolean onContextItemSelected(MenuItem item) {  
         if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}  
         else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
         else return false; 
     return true;  
     }  

     public void function1(int id){ 
         if (saveringifightsong(R.raw.ifightsong)){  
             // Code if successful  
             Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
             }  
             else  
             {  
             // Code if unsuccessful  
             Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
             }

     }

     public void function2(int id){  
         if (savenotifightsong(R.raw.ifightsong)){  
             // Code if successful  
             Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
             }  
             else  
             {  
             // Code if unsuccessful  
             Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
             }}



     public boolean saveringifightsong(int ressound){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
         int size=0;
           try {  
                size = fIn.available();  
                buffer = new byte[size];  
                fIn.read(buffer);  
                fIn.close();  
               } catch (IOException e) {  
                // TODO Auto-generated catch block  
                return false;  
               }
                String path="/sdcard/media/audio/ringtones/";  
                String filename="ifightsong_ring"+".ogg"; 

           boolean exists = (new File(path)).exists();  
           if (!exists){new File(path).mkdirs();}  

           FileOutputStream save;  
           try {  
            save = new FileOutputStream(path+filename);  
            save.write(buffer);  
            save.flush();  
            save.close();  
           } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            return false;  
           } catch (IOException e) {  
            // TODO Auto-generated catch block  
            return false;  
           }      

           sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

           File k = new File(path, filename);  

           ContentValues values = new ContentValues();  
           values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
           values.put(MediaStore.MediaColumns.TITLE, "Iowa Fight Song");  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
           values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");  
           values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
           values.put(MediaStore.Audio.Media.IS_ALARM, false);  
           values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

           //Insert it into the database  
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

            Uri newUri = getContentResolver().insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(
                    HawkeyeAddictSoundboardActivity.this,
              RingtoneManager.TYPE_RINGTONE,
              newUri
            );
            return true;  
          }   
     public boolean savenotifightsong(int ressound){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
         int size=0;
           try {  
                size = fIn.available();  
                buffer = new byte[size];  
                fIn.read(buffer);  
                fIn.close();  
               } catch (IOException e) {  
                // TODO Auto-generated catch block  
                return false;  
               }
                String path="/sdcard/media/audio/ringtones/";  
                String filename="ifightsong_not"+".ogg"; 

           boolean exists = (new File(path)).exists();  
           if (!exists){new File(path).mkdirs();}  

           FileOutputStream save;  
           try {  
            save = new FileOutputStream(path+filename);  
            save.write(buffer);  
            save.flush();  
            save.close();  
           } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            return false;  
           } catch (IOException e) {  
            // TODO Auto-generated catch block  
            return false;  
           }      

           sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

           File k = new File(path, filename);  

           ContentValues values = new ContentValues();  
           values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
           values.put(MediaStore.MediaColumns.TITLE, "Iowa Fight Song");  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
           values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");  
           values.put(MediaStore.Audio.Media.IS_RINGTONE, false);  
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
           values.put(MediaStore.Audio.Media.IS_ALARM, false);  
           values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

           //Insert it into the database  
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

            Uri newUri = getContentResolver().insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(
                    HawkeyeAddictSoundboardActivity.this,
              RingtoneManager.TYPE_NOTIFICATION,
              newUri
            );
            return true;  
          }
     public boolean saveringoniowa(int ressound){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
         int size=0;
           try {  
                size = fIn.available();  
                buffer = new byte[size];  
                fIn.read(buffer);  
                fIn.close();  
               } catch (IOException e) {  
                // TODO Auto-generated catch block  
                return false;  
               }
                String path="/sdcard/media/audio/ringtones/";  
                String filename="oniowa_ring"+".ogg"; 

           boolean exists = (new File(path)).exists();  
           if (!exists){new File(path).mkdirs();}  

           FileOutputStream save;  
           try {  
            save = new FileOutputStream(path+filename);  
            save.write(buffer);  
            save.flush();  
            save.close();  
           } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            return false;  
           } catch (IOException e) {  
            // TODO Auto-generated catch block  
            return false;  
           }      

           sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

           File k = new File(path, filename);  

           ContentValues values = new ContentValues();  
           values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
           values.put(MediaStore.MediaColumns.TITLE, "On Iowa");  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
           values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");  
           values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
           values.put(MediaStore.Audio.Media.IS_ALARM, false);  
           values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

           //Insert it into the database  
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

            Uri newUri = getContentResolver().insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(
                    HawkeyeAddictSoundboardActivity.this,
              RingtoneManager.TYPE_RINGTONE,
              newUri
            );
            return true;  
          }
     public boolean savenotoniowa(int ressound){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
         int size=0;
           try {  
                size = fIn.available();  
                buffer = new byte[size];  
                fIn.read(buffer);  
                fIn.close();  
               } catch (IOException e) {  
                // TODO Auto-generated catch block  
                return false;  
               }
                String path="/sdcard/media/audio/ringtones/";  
                String filename="oniowa_not"+".ogg"; 

           boolean exists = (new File(path)).exists();  
           if (!exists){new File(path).mkdirs();}  

           FileOutputStream save;  
           try {  
            save = new FileOutputStream(path+filename);  
            save.write(buffer);  
            save.flush();  
            save.close();  
           } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            return false;  
           } catch (IOException e) {  
            // TODO Auto-generated catch block  
            return false;  
           }      

           sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

           File k = new File(path, filename);  

           ContentValues values = new ContentValues();  
           values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
           values.put(MediaStore.MediaColumns.TITLE, "On Iowa");  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
           values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");  
           values.put(MediaStore.Audio.Media.IS_RINGTONE, false);  
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
           values.put(MediaStore.Audio.Media.IS_ALARM, false);  
           values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

           //Insert it into the database  
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

            Uri newUri = getContentResolver().insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(
                    HawkeyeAddictSoundboardActivity.this,
              RingtoneManager.TYPE_NOTIFICATION,
              newUri
            );
            return true;  
          }

}

EDIT
Please have a look at my getTag for contextmenu below. The question is, am I close Ted?

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
                    int tag;
                    try {
                        tag = Integer.parseInt((String) v.getTag());
                    } catch (Exception ex) {
                        // null or invalid tag -- how'd that happen?
                        return;
                    }
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Set As...");
     menu.add(0, soundArray[tag], 0, "Ringtone");
     menu.add(0, soundArray[tag], 0, "Notification");
     }

     @Override  
     public boolean onContextItemSelected(MenuItem item) {  
         if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}  
         else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
         else return false; 
     return true;  
     }  


     public void function1(int tag){ 
             if (savering(soundArray[tag])){  
             // Code if successful  
             Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
             }  
             else  
             {  
             // Code if unsuccessful  
             Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
             }

     }
     public void function2(int tag){  

         if (savenot(soundArray[tag])){  
             // Code if successful  
             Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
             }  
             else  
             {  
             // Code if unsuccessful  
             Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).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-05-23T11:26:20+00:00Added an answer on May 23, 2026 at 11:26 am

    This code makes no sense. This statement:

    int [] = {R.raw.sound1, R.raw.sound2};
    

    isn’t legal Java—no variable name.

    The body of your for loop isn’t a statement (it doesn’t do anything), so that’s not legal either.

    The case values need to be compile time constants. You cannot use a case to select among different run-time values. You’ll have to re-do the switch as something like this:

    int id = v.getId();
    for (int button : buttonArray) {
        if (id == button) {
            // got a hit -- process it
            break; // then end the for loop
        }
    }
    

    EDIT

    Okay, I think I see what you’re trying to do. When I do this kind of thing, I find it useful to use the android:tag attribute; it’s much more convenient than what you’re trying to do. You can set the tag in the layout or do it in code. Here’s a method that does it in code.

    int[] soundArray = { /* ... */ };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
        fightsong = (Button) findViewById(R.id.fightsong_button);
        fightsong.setOnClickListener(this);
        fightSong.setTag("0");
        oniowa = (Button) findViewById(R.id.oniowa_button);
        oniowa.setOnClickListener(this);
        oniowa.setTag("1");
        registerForContextMenu(fightsong);  
        registerForContextMenu(oniowa); 
    }
    
    @Override
    public void onClick(View v) {
        int tag;
        try {
            tag = Integer.parseInt(v.getTag());
        } catch (Exception ex) {
            // null or invalid tag -- how'd that happen?
            return;
        }
        // clear out old media player (if any)
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            mMediaPlayer.release();
        }
        // fire up a media player with the correct sound
        mMediaPlayer = MediaPlayer.create(this, soundArray[tag]);
        mMediaPlayer.start();
    }
    

    You can also use the tag instead of the view id when creating the context menu.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.