By Alvin Alexander. Last updated: August 12, 2018
Android Button FAQ: How do I add a listener to a Button in Android?
To add a click (tap) listener to a button in Android, just get a reference to the button (typically using findViewById), then call the setOnClickListener method of the button, like this:
Button saveButton = (Button) findViewById(R.id.save_button); saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do the "on click" action here } });
As you can see from that source code, all you have to do is put your code in the onClick
method to handle the button tap/click action.
For more information on handling Button clicks/taps, see the official Android developer docs: