Search code examples
androidandroid-widget

Disable Button on click


I am newbie to the programming world and my knowledge is limited. Please excuse me if i ask any blunder. My question is that.

I am creating an Activity which has START & STOP button. when user clicks on START button a service must start; and on STOP service must stop.

Now I want to disable my START button when i Click start button(service starts on click START button) and when clicks STOP button i want to see the START button as normal clickable button.

I have used .setEnabled(false) by creating the button object. i need help...Thanks in advance


Solution

  • int count = 0;
    
    if (count == 0) {
        stop.setEnabled(false);
        PlayButton.setEnabled(true);
    }
    
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.play:
                count++;
                play.setEnabled(false);
                Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
                Stopbutton.setEnabled(true);
                break;
    
            case R.id.stop:
                Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
                count--;    
                PlayButton.setEnabled(true);
                stop.setEnabled(false);
                break;        
        }
    }
    

    & check this link How to disable an Android button?