Android Button Using the layout to define a click action

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks.

Button

<Button
    android:width="120dp"
    android:height="wrap_content"
    android:text="Click me"
    android:onClick="handleClick" />

Then in your activity, create the handleClick method.

public void handleClick(View v) {
    // Do whatever.
}


Got any Android Question?