Disable button when pressed

I am new to the world of programming and my knowledge is limited. Please excuse me if I ask for any mistake. My question is that.

I am creating an Activity that has a START and STOP button. when the user presses the "START" button, the service should start; and the STOP service should stop.

Now I want to disable the "START" button when I press the "Start" button (the service starts when the "START" button is pressed), and when I press the "STOP" button, I want to see the "START" button as a normal click button.

I used .setEnabled (false), creating a button object. I need help ... Thanks in advance

+8
android android widget
source share
10 answers
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 the Android button?

+10
source share

You can also try: -

to enable the button -

 button.setClickable(true); 

to disable the button -

 button.setClickable(false); 
+6
source share

in body onclick

disable button1 when pressed.

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

Try the following:

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 main.xml layout

 <?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" /> 

+1
source share

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

NOTE. If you want the button to be invisible and not require that it consume the layout space that it requires, you can use View.GONE instead of View.INVISIBLE

I hope I get it.

+1
source share

If you want to disconnect it from another class, you can use it,

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

You should 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 the 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); } 
+1
source share

You can call button.setOnClickListener(null); to cancel the event list. In addition, you can change the background selection to give it a disabled effect.

PS: Use this solution only when nothing works.

0
source share

more preferred solution is

 onclick(){ btn.setEnabled(false); btn.setClickable(false); //yourwork myWork(); } myWork(){ //your tasks. btn.setEnabled(true); btn.setClickable(true); } 
0
source share

initialize onClickListener for the button. Inside the fist button, simply set the setEnable () parameter to false .. and from the second set of buttons, setEnable to true

to use

0
source share
 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 it, work fine

0
source share

All Articles