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); }
Newb2java
source share