19

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

Code_Life
  • 5,742
  • 4
  • 29
  • 49
ADD
  • 195
  • 1
  • 1
  • 6
  • 1
    what happens when u use .setEnabled(false) ? Can you be a little more specific as to what output you are expecting and what you are getting – Nav Mar 23 '12 at 12:03
  • Nav-its seen as the button is disabled. But i want the button to be disabled only when i press that button – ADD Mar 23 '12 at 12:13
  • Huh? You want to disable the Start button when it has been clicked, right? And when the Stop button is clicked you want to disable the Stop button and enable the Start button, or am I all wrong? – Araw Mar 23 '12 at 12:17

11 Answers11

15
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?

Community
  • 1
  • 1
Amandeep singh
  • 1,865
  • 7
  • 21
  • 41
10

You can also try:-

for button enable-

button.setClickable(true); 

for button disable-

button.setClickable(false);
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
sudhishkr
  • 3,318
  • 5
  • 33
  • 55
3

in the body of onclick

disable button1 as it get clicked

public void onClick(View v) {
  if(v.getId() == R.id.button1)
   {
       Button btn = (Button)findViewById(R.id.buton1);
       btn.setEnabled(false);

   }

}
vipin
  • 2,851
  • 4
  • 18
  • 32
2

If you want to disable it from another class you can use this,

Button btn = ((MainActivity)context).findViewById(R.id.myButton);
btn.setEnabled(false);    //or (true) to enable it

You must also declare 'context' at the beginning of your class

public class MyClass extends AppCompatActivity {    
    Context context;

I usually use it in my onPreExecute and onPostExecute when I need to perform an action and don't want a user to keep clicking the button.

@Override
protected void onPreExecute() {
    //some actions to be performed or set before executing task 
    Button btn = ((MainActivity)context).findViewById(R.id.myButton);
    btn.setEnabled(false);
}

@Override
protected void onPostExecute() {
    //some actions to be performed or set after executing task 
    Button btn = ((MainActivity)context).findViewById(R.id.myButton);
    btn.setEnabled(true);
}
2

With kotlin you disable a button on click with,

myButton.setOnClickListener {
    it.isClickable = false     // to disable clicking on button
    it.isEnabled = false       // to disable button
}

Don't forget that the view here is it

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
1

more preferred solution is,

onclick(){
  btn.setEnabled(false);
  btn.setClickable(false);
  //yourwork
  myWork();
}

myWork(){
 //your tasks.
 btn.setEnabled(true);
 btn.setClickable(true);
}
Narasimha
  • 759
  • 6
  • 8
1

initialise onClickListener for the button.inside the fist button simply do setEnable() to false ..and from the second button click listener set setEnable to true

enjoy

Jeekiran
  • 473
  • 3
  • 10
1

Try this:

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;

public class MainActivity extends Activity {

private Button start, stop;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    start = (Button)findViewById(R.id.start);
    stop = (Button)findViewById(R.id.stop);

    start.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            start.setVisibility(View.GONE);
            /* do something else */
        }
    });

    stop.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            start.setVisibility(View.VISIBLE);
            /* do something else */
        }
    });
}

}

And your layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/start"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Start"
android:visibility="visible"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Stop"
android:visibility="visible"
/>

Marcos
  • 4,643
  • 7
  • 33
  • 60
1

If you wanna make the button invisible after button cleck then 1st disable it as vipin said and also add this .setVisibility(View.INVISIBLE); this will hide the button after the button click and when you want to again make it visible use this .setVisibility(View.VISIBLE);

NOTE: if you want the button to be invisible and not don't want it to consume the layout space it requires then you can use View.GONE instead of View.INVISIBLE

I hope I am clear.

Nav
  • 10,304
  • 20
  • 56
  • 83
0

You can call button.setOnClickListener(null); to cancel the event listner. Additionally you can change the background drawable to give it a disabled effect.

PS: Only try this solution when nothing else works.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
-1
myButton.setEnabled(false);

Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {

    @Override
    public void run() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                myButton.setEnabled(true);
            }
        });
    }
}, 5000);

try this it,s work perfectly

Daniel
  • 9,491
  • 12
  • 50
  • 66
vani
  • 1