Disable Button on click
Asked Answered
B

11

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

Brennan answered 23/3, 2012 at 12:0 Comment(3)
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 gettingHesterhesther
Nav-its seen as the button is disabled. But i want the button to be disabled only when i press that buttonBrennan
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?Bohi
H
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?

Hoofed answered 23/3, 2012 at 13:4 Comment(0)
P
10

You can also try:-

for button enable-

button.setClickable(true); 

for button disable-

button.setClickable(false);
Philipps answered 30/12, 2013 at 17:3 Comment(1)
Thanks, this is what i exactly want, +1 for itRigel
T
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);

   }

}
Taskwork answered 23/3, 2012 at 12:21 Comment(0)
W
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);
}
Wrapped answered 25/1, 2018 at 21:52 Comment(0)
T
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

Terwilliger answered 2/3, 2020 at 18:15 Comment(0)
Q
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"
/>

Quartan answered 23/3, 2012 at 12:29 Comment(0)
H
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.

Hesterhesther answered 23/3, 2012 at 12:30 Comment(0)
M
1

more preferred solution is,

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

myWork(){
 //your tasks.
 btn.setEnabled(true);
 btn.setClickable(true);
}
Mendez answered 17/9, 2013 at 23:51 Comment(0)
C
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

Crabbed answered 19/6, 2014 at 13:58 Comment(0)
I
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.

Impercipient answered 23/3, 2012 at 12:25 Comment(1)
Although more preffered solution is to use setEnabled(false) method.Impercipient
H
-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

Hebert answered 17/3, 2016 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.