Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. How do I set all buttons clickable or unclickable at same time using setClickable?

The screen displays four buttons. When a button is pressed, a media player plays a sound. The problem I'm having is implementing setClickable for all buttons at the same time.

Once a button is clicked, I want all buttons to be unclickable until the media player is finished playing the sound associated with the button click. Then I want all buttons to be set back to clickable.

The code runs fine until I enable the setClickable code--the code for buttonOne is disabled in my code sample below. The test phone locks up and tells me the application has stopped and to try again.

Unfortunately, without setClickable, the user could press any button and hear any sound before the first selected sound is finished playing.

Thank you for your time and help.

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;


public class hipsterdoofus  extends Activity
{
 private int asoundfilenumber;//integer id of sound file to be played

 public ImageButton buttonOne;
 public ImageButton buttonTwo;
 public ImageButton buttonThree;
 public ImageButton buttonFour;


 public void myClickHandler(View v) {



    switch (v.getId())
       {

        case R.id.buttonOne:
         asoundfilenumber=0x7f040000;
         break;

        case R.id.buttonTwo:
         asoundfilenumber=0x7f040001;
         break;

        case R.id.buttonThree:
         asoundfilenumber=0x7f040002;
         break;

        case R.id.buttonFour:
         asoundfilenumber=0x7f040003;
         break;   



        }//closes switch test



    freezeButtonsAndPlaySoundThenUnfreezeButtons();

  }//closes onClick


  public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
 {

  **//buttonOne.setClickable( false );//sets buttonOne to unclickable**

  MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
  mp.start();


  mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
   {

   @Override
   public void onCompletion(MediaPlayer mpalmost) 
    {
    mpalmost.release();
    }



   });

  **//buttonOne.setClickable( true ); //sets buttonOne to clickable**

 }


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

    }
like image 918
John Avatar asked Sep 15 '10 15:09

John


People also ask

How does android handle multiple clicks?

Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.

How do you add functionality to a button in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How do I stop multiple clicks on android?

Here's a OnClickListener proxy that prevents successive clicks, based on qezt's answer. button. setOnClickListener(new MultiClickGuard(new View. OnClickListener() { @Override public void onClick(View v) { // do something } }, 1000)); button.


1 Answers

i think the property you are looking for would be setEnabled (set with boolean)

some code;

public void enableDisableButtons(Boolean state){
    buttonOne.setEnabled(state);
    buttonTwo.setEnabled(state);
    buttonThree.setEnabled(state);
    buttonFour.setEnabled(state);
}

public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
{
    enableDisableButtons(false); // disable buttons

    MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
    mp.start();


    mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
    {

        @Override
        public void onCompletion(MediaPlayer mpalmost) 
        {
            enableDisableButtons(true); // Re-enable buttons
            mpalmost.release();
        }
      });
}
like image 159
Damon Skelhorn Avatar answered Oct 05 '22 23:10

Damon Skelhorn