Android How to set all buttons that can be clicked or disabled at the same time using setClickable?

Four buttons are displayed on the screen. When a button is pressed, the media player plays a sound. The problem I am facing is that it implements setClickable for all buttons at the same time.

After pressing the button, I want all the buttons to be invisible until the media player finishes playing the sound associated with the button click. Then I want all the buttons to be set back to interactive.

The code works fine until I turn on the setClickable code - the code for buttonOne is disabled in my code example below. The test phone is blocked and informs me that the application has stopped and try again.

Unfortunately, without setClickable, the user can press any button and hear a sound before the first selected sound ends.

Thanks 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 ); } 
+6
android button click clickable
source share
2 answers

I think the property you are looking for will be set to Enabled (set using 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(); } }); } 
+4
source share

If you want to disable the button, use the setEnabled (false) method of the View class

+1
source share

All Articles