-2

I have a view (button) and am listening to a click event, but in my while loop it won't call the onClick method:

public class Activity_Main extends Activity implements OnClickListener {
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.btn_1); 
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                //do something make boolean false for example
                break;
        }
    }

    while(boolean == true) {
        //it's not reacting on the click
    }

Is there something I am missing?

admdrew
  • 3,790
  • 4
  • 27
  • 39
Diego
  • 4,011
  • 10
  • 50
  • 76

2 Answers2

1

You are blocking the main thread with that infinite while loop:

while(boolean == true) {
    //it's not reacting on the click
}

and it cant execute any other code. You should be more worried about the ANR coming next rather then the click not responding. Put the while loop in a separate thread as suggested and dont overload the main thread's work with excessive tasks.

eldjon
  • 2,800
  • 2
  • 20
  • 23
1

If you use Handlers and Messages you can start and stop the recording when the user clicks the button. And to check if the button has been clicked, just use onClick method.

mthandr
  • 3,062
  • 2
  • 23
  • 33