An Android Button click (tap) listener example

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:

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.