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];
}
}
The
onCreateContextMenu()method has a parameter namedView vthat represent theViewfor which theContextMenuis being built. So from this you could get the ID (v.getId()) and then, like you did in theView.OnClickListener listener, find out the position in thebuttonIdsarray and then set theselectedSoundIdaccordingly.Also this:
should be something like this:
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.