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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:38:55+00:00 2026-05-15T22:38:55+00:00

What I have here real simple activity with two buttons. When you press each

  • 0

What I have here real simple activity with two buttons. When you press each of them it plays a sound.

When i press and hold the first button it brings up a context menu asking the user if they want to save the sound as a ringtone or notification. This works perfectly on the first button.

The second button’s sound plays when pressed. When long pressed it brings up the context menu….BUT it saves the first sound file as ringtone/notification NOT the second…

Would someone be able to shed some insight on why the second context menu isn’t working properly?

package com.my.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import android.view.ContextMenu.ContextMenuInfo;  
import android.widget.Button;  
import android.widget.Toast; 
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;


public class One extends Activity implements OnClickListener{

    private SoundManager mSoundManager;


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

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.blah);
        mSoundManager.addSound(2, R.raw.rofl);


//BUTTONS PLAY SOUND WHEN PRESSED

        View SoundButton1 = findViewById(R.id.Sound1);
        SoundButton1.setOnClickListener(this);

        View SoundButton2 = findViewById(R.id.Sound2);
        SoundButton2.setOnClickListener(this);
 }

            public void onClick(View v) {
                switch (v.getId()) {

                case R.id.Sound1:
        mSoundManager.playSound(1);
                break;

            case R.id.Sound2:
    mSoundManager.playSound(2);
                break;
    }

//WHEN LONG PRESSED BUTTONS BRING UP CONTEXT MENU FOR SAVE AS RINGTONE OR NOTIFICATION

        Button SoundButton11 = (Button) findViewById(R.id.Sound1);  
        registerForContextMenu(SoundButton11);  

        Button SoundButton22 = (Button) findViewById(R.id.Sound2);  
        registerForContextMenu(SoundButton22);  
    }  
//CONTEXT MENU FOR BUTTON 1
    @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");  
    }  

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

    public void function1(int id){ 
        if (savering(R.raw.blah)){  
            // 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 (savenot(R.raw.blah)){  
            // 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();
            }




//CONTEXT MENU FOR BUTTON 2 
    }

    public void onCreateContextMenu1(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");  
    }  

    public boolean onContextItemSelected1(MenuItem item) {  
        if(item.getTitle()=="Ringtone"){function11(item.getItemId());}  
        else if(item.getTitle()=="Notification"){function21(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    public void function11(int id){ 
        if (savering(R.raw.rofl)){  
            // 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 function21(int id){  
        if (savenot(R.raw.rofl)){  
            // 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 savering(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="HahaSound"+".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, "HahaSound");  
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
       values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
       values.put(MediaStore.Audio.Media.IS_ALARM, true);  
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

       //Insert it into the database  
       this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

       return true;  
      }  

   public boolean savenot(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/notifications/";  
       String filename="HahaSound"+".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, "HahaSoundSound");  
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
       values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
       values.put(MediaStore.Audio.Media.IS_RINGTONE, false);  
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
       values.put(MediaStore.Audio.Media.IS_ALARM, true);  
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

       //Insert it into the database  
       this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

       return true;  
      }  
}

UPDATED SINCE STOLE’S RESPONSE-

//BUTTON 1

        View SoundButton1 = findViewById(R.id.Sound1);
        SoundButton1.setOnClickListener(this);

        View SoundButton2 = findViewById(R.id.Sound2);
        SoundButton2.setOnClickListener(this);
 }

            public void onClick(View v) {
                switch (v.getId()) {

                case R.id.Sound1:
        mSoundManager.playSound(1);
                break;

            case R.id.Sound2:
        mSoundManager.playSound(2);
                break;
                }

                Button SoundButton11 = (Button) findViewById(R.id.Sound1);  
                registerForContextMenu(SoundButton11);  

                Button SoundButton22 = (Button) findViewById(R.id.Sound2);  
                registerForContextMenu(SoundButton22);  
            }

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

    public boolean onContextItemSelected(MenuItem item) { 
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        long SoundButton11 = info.id;
        switch (item.getItemId()) { 
    case MENU_RINGTONE:
        if (savering(R.raw.schwing)){  
            // 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();
            }
            break;
    case MENU_NOTIFICATION:
        if (savenot(R.raw.schwing)){  
            // 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();
            }
            break;
    }
        return false;


    } 

This is what ‘ve got since reading your last response. I’m just trying to get it working on the first button before i try moving onto the next. But with your code i’m getting a warning under SoundButton11 “The local variable SoundButton11 is never read”
I’m confused because I have…

Button SoundButton11 = (Button) findViewById(R.id.Sound1);  
                    registerForContextMenu(SoundButton11); 

I also tried Sound1 and that did not work either. Any suggestions?

  • 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-15T22:38:56+00:00Added an answer on May 15, 2026 at 10:38 pm

    2 Things about your code….

    1) There’s no function called onCreateContextMenu1 so you’re not overriding it…So the first onCreateContextMenu is called.

    2)

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

    You’re assigning both menu item (Context) the same id…they ideally have to be different. how else would you identify them. I’m suprised its actually working for you, using titles.

    here’s what you should be doing…

    final int MENU_RINGTONE = 0;
    final int MENU_NOTIFICATION = 1;
    
    public void onCreateContextMenu(ContextMenu menu, View v, 
                ContextMenuInfo menuInfo) { 
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(0, MENU_RINGTONE, 0, "Ringtone"); 
            menu.add(0, MENU_NOTIFICATION, 0, "Notification"); 
    } 
    
    public boolean onContextItemSelected(MenuItem item) { 
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            long buttonId = info.id;
            switch (item.getItemId()) { 
        case MENU_RINGTONE:
                function1(buttonId);
                break;
        case MENU_NOTIFICATION:
                function2(buttonId);
                break;
        }
    }
    

    you don’t need the additional function12,funtion11,function21,function22…you can generalize them…but i leave that upto you.

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

Sidebar

Related Questions

I have a real simple form with a textbox and a button, and my
I have a real simple question here. I only want to know if there
I have a URI here in which a simple document.cookie query through the console
I know this should be real simple, but I have googled this problem and
I'm new to jQuery so this may be a real simple answer. I have
I have read several interesting C#/F# comparisons here on stackoverflow. So, first of all,
I have something here that is really catching me off guard. I have an
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
Just wondering what little scripts/programs people here have written that helps one with his
Since upgrading to 2008 I and many people here have noticed that randomly VS

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.