views:

382

answers:

3

Hello guys!

I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where setting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?

Thanks in advance!

playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
    public void onClick(View v) {
 playBtn.setVisibility(ImageButton.GONE);
 pauseBtn.setVisibility(ImageButton.VISIBLE);
 mp.start();
    }});

pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
 public void onClick(View v) {
  pauseBtn.setVisibility(ImageButton.GONE);
  playBtn.setVisibility(ImageButton.VISIBLE);
  mp.pause();
 }});

final class SeekBarTask extends TimerTask {
  public SeekBarTask(int duration) {

  }
  @Override
  public void run() {
   if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
    pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
    playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
    mp.stop();
   }
   else {
   seekBar.incrementProgressBy(100);
   }
  }
 }
A: 

I would recommend just changing the icon of one ImageButton.

CommonsWare
A: 

I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.

Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?

I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.

Rich
A: 

I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?

JRL