how to add button click event in android studio

To add a button click event in Android Studio, follow these steps:

  1. Open your project in Android Studio and go to the XML layout file for the activity where you want to add the button.
  2. In the layout file, add a Button element. You can do this by dragging the Button element from the Palette pane on the left side of the screen, or by manually adding the XML code for the Button element.
  3. Give the Button a unique id attribute, which you can use to reference the button in your Java code.
  4. In your activity's Java code, find the onCreate method and add code to find the button by its id and set an OnClickListener for it.

Here's an example of how to add a button click event in Android Studio:

public class MainActivity extends AppCompatActivity {

  protected void onCreate(Bundle savedInstanceState) {

    Button button = (Button) findViewById(;
    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        // code to be executed when button is clicked

In this example, the onCreate method sets an OnClickListener for the button with the id "button". When the button is clicked, the code inside the onClick method will be executed.

I hope this helps! Let me know if you have any questions.