Let your activity implement View.OnClickListener , and then you must add the onClick(View view) method.
When creating buttons, you simply add this line
button.setOnClickListener(this) , so whenever a button is clicked, it calls the onClick() method in your activity.
Inside the onClick() method, you can use view.getId() to find out which button was clicked.
Code:
// onCreate () - either dynamically create your own buttons, or find them using findViewById () and assign a click listener
for(int i = 0; i < MAX_BUTTONS; i++){
// onClick ()
switch(view.getId(){ case R.id.btn_one: // do something break; ... }
source share