<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myFancyMethod" />
<!-- even more layout elements -->
The above code just shows the XML implementation. Now in the background Android does nothing else than the Java code calling your method on a click event.
Note that with the XML above, Android will look for the onClick method myFancyMethod() only in the currently active Activity's .java file. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
}
public void myFancyMethod(final View view) {
System.out.println("clicked");
}